72 lines
2.7 KiB
Java
72 lines
2.7 KiB
Java
package com.cygnus.client;
|
|
|
|
import java.net.URI;
|
|
import java.time.Duration;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public record CloudClientProperties(
|
|
URI baseUri,
|
|
URI tokenUri,
|
|
String clientId,
|
|
String installationId,
|
|
String clientAssertion,
|
|
String loginKeyId,
|
|
String loginPublicKeyLocation,
|
|
Duration requestTimeout,
|
|
Duration tokenRefreshSkew) {
|
|
|
|
public static CloudClientProperties fromSystem() {
|
|
CloudClientProperties properties = new CloudClientProperties(
|
|
URI.create(value("CYGNUS_CLOUD_BASE_URL", "http://localhost:8090")),
|
|
URI.create(value(
|
|
"CYGNUS_TOKEN_URL",
|
|
"http://localhost:8090/oauth2/token")),
|
|
value("CYGNUS_CLIENT_ID", ""),
|
|
value("CYGNUS_INSTALLATION_ID", ""),
|
|
value("CYGNUS_CLIENT_ASSERTION", ""),
|
|
value("CYGNUS_LOGIN_KEY_ID", "cygnus-login-2026-01"),
|
|
value(
|
|
"CYGNUS_LOGIN_PUBLIC_KEY",
|
|
"file:./config/keys/login-public.pem"),
|
|
Duration.parse(value("CYGNUS_CLOUD_REQUEST_TIMEOUT", "PT10S")),
|
|
Duration.parse(value("CYGNUS_TOKEN_REFRESH_SKEW", "PT30S")));
|
|
properties.validate();
|
|
return properties;
|
|
}
|
|
|
|
public void validate() {
|
|
List<String> missing = new ArrayList<>();
|
|
require(clientId, "CYGNUS_CLIENT_ID", missing);
|
|
require(installationId, "CYGNUS_INSTALLATION_ID", missing);
|
|
require(clientAssertion, "CYGNUS_CLIENT_ASSERTION", missing);
|
|
require(loginKeyId, "CYGNUS_LOGIN_KEY_ID", missing);
|
|
require(loginPublicKeyLocation, "CYGNUS_LOGIN_PUBLIC_KEY", missing);
|
|
if (!missing.isEmpty()) {
|
|
throw new IllegalStateException(
|
|
"Cloud identity requires these settings: "
|
|
+ String.join(", ", missing));
|
|
}
|
|
if (requestTimeout.isZero() || requestTimeout.isNegative()) {
|
|
throw new IllegalStateException(
|
|
"CYGNUS_CLOUD_REQUEST_TIMEOUT must be greater than zero");
|
|
}
|
|
}
|
|
|
|
private static void require(String value, String name, List<String> missing) {
|
|
if (value == null || value.isBlank()) {
|
|
missing.add(name);
|
|
}
|
|
}
|
|
|
|
private static String value(String name, String defaultValue) {
|
|
String systemValue = System.getProperty(name);
|
|
if (systemValue != null) {
|
|
return systemValue;
|
|
}
|
|
String environmentValue = System.getenv(name);
|
|
return environmentValue == null ? defaultValue : environmentValue;
|
|
}
|
|
|
|
}
|