|
| 1 | +package cloud.stackit.sdk.core.auth; |
| 2 | + |
| 3 | +import cloud.stackit.sdk.core.KeyFlowAuthenticator; |
| 4 | +import cloud.stackit.sdk.core.config.Configuration; |
| 5 | +import cloud.stackit.sdk.core.config.EnvironmentVariables; |
| 6 | +import cloud.stackit.sdk.core.KeyFlowInterceptor; |
| 7 | +import cloud.stackit.sdk.core.model.ServiceAccountKey; |
| 8 | +import com.google.gson.Gson; |
| 9 | +import com.google.gson.reflect.TypeToken; |
| 10 | +import okhttp3.Interceptor; |
| 11 | + |
| 12 | +import java.lang.reflect.Type; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +public class SetupAuth { |
| 19 | + private Interceptor authHandler; |
| 20 | + private final String defaultCredentialsFilePath = "~/.stackit/credentials.json"; |
| 21 | + |
| 22 | + public SetupAuth() { |
| 23 | + this(new Configuration.Builder().build()); |
| 24 | + } |
| 25 | + |
| 26 | + public SetupAuth(Configuration cfg) { |
| 27 | + if (cfg == null) { |
| 28 | + cfg = new Configuration.Builder().build(); |
| 29 | + } |
| 30 | + |
| 31 | + try { |
| 32 | + ServiceAccountKey saKey = setupKeyFlow(cfg); |
| 33 | + authHandler = new KeyFlowInterceptor(new KeyFlowAuthenticator(saKey)); |
| 34 | + } catch (Exception e) { |
| 35 | + e.printStackTrace(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public Interceptor getAuthHandler() { |
| 40 | + return authHandler; |
| 41 | + } |
| 42 | + |
| 43 | + private ServiceAccountKey setupKeyFlow(Configuration cfg) throws Exception { |
| 44 | + ServiceAccountKey saKey = null; |
| 45 | + // Explicit config in code |
| 46 | + if (cfg.getServiceAccountKey() != null && !cfg.getServiceAccountKey().trim().isEmpty()) { |
| 47 | + saKey = ServiceAccountKey.loadCredentials(cfg.getServiceAccountKey()); |
| 48 | + loadPrivateKey(cfg, saKey); |
| 49 | + return saKey; |
| 50 | + } |
| 51 | + |
| 52 | + if (cfg.getServiceAccountKeyPath() != null && !cfg.getServiceAccountKeyPath().trim().isEmpty()) { |
| 53 | + String fileContent = new String(Files.readAllBytes(Paths.get(cfg.getServiceAccountKeyPath())), StandardCharsets.UTF_8); |
| 54 | + saKey = new Gson().fromJson(fileContent, ServiceAccountKey.class); |
| 55 | + loadPrivateKey(cfg, saKey); |
| 56 | + return saKey; |
| 57 | + } |
| 58 | + |
| 59 | + // Env config |
| 60 | + if (!EnvironmentVariables.STACKIT_SERVICE_ACCOUNT_KEY.trim().isEmpty()) { |
| 61 | + saKey = ServiceAccountKey.loadCredentials(EnvironmentVariables.STACKIT_SERVICE_ACCOUNT_KEY.trim()); |
| 62 | + loadPrivateKey(cfg, saKey); |
| 63 | + return saKey; |
| 64 | + } |
| 65 | + |
| 66 | + if (!EnvironmentVariables.STACKIT_SERVICE_ACCOUNT_KEY_PATH.trim().isEmpty()) { |
| 67 | + String fileContent = new String(Files.readAllBytes(Paths.get(cfg.getServiceAccountKeyPath())), StandardCharsets.UTF_8); |
| 68 | + saKey = new Gson().fromJson(fileContent, ServiceAccountKey.class); |
| 69 | + loadPrivateKey(cfg, saKey); |
| 70 | + return saKey; |
| 71 | + } |
| 72 | + |
| 73 | + if (!EnvironmentVariables.STACKIT_CREDENTIALS_PATH.trim().isEmpty()) { |
| 74 | + String saKeyJson = readValueFromCredentialsFile(EnvironmentVariables.STACKIT_CREDENTIALS_PATH, EnvironmentVariables.ENV_STACKIT_SERVICE_ACCOUNT_KEY, EnvironmentVariables.ENV_STACKIT_SERVICE_ACCOUNT_KEY_PATH); |
| 75 | + saKey = new Gson().fromJson(saKeyJson, ServiceAccountKey.class); |
| 76 | + loadPrivateKey(cfg, saKey); |
| 77 | + return saKey; |
| 78 | + } else { |
| 79 | + try { |
| 80 | + String saKeyJson = readValueFromCredentialsFile(defaultCredentialsFilePath, EnvironmentVariables.ENV_STACKIT_SERVICE_ACCOUNT_KEY, EnvironmentVariables.ENV_STACKIT_SERVICE_ACCOUNT_KEY_PATH); |
| 81 | + saKey = new Gson().fromJson(saKeyJson, ServiceAccountKey.class); |
| 82 | + loadPrivateKey(cfg, saKey); |
| 83 | + return saKey; |
| 84 | + } catch (Exception e) { |
| 85 | + throw new Exception("could not find service account key"); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void loadPrivateKey(Configuration cfg, ServiceAccountKey saKey) throws Exception { |
| 91 | + if (!saKey.getCredentials().isPrivateKeySet()) { |
| 92 | + try { |
| 93 | + String privateKey = getPrivateKey(cfg); |
| 94 | + saKey.getCredentials().setPrivateKey(privateKey); |
| 95 | + } catch (Exception e) { |
| 96 | + throw new Exception("could not find private key", e); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private String getPrivateKey(Configuration cfg) throws Exception { |
| 102 | + // Explicit code config |
| 103 | + // Set private key |
| 104 | + if (cfg.getPrivateKey() != null && !cfg.getPrivateKey().trim().isEmpty()) { |
| 105 | + return cfg.getPrivateKey(); |
| 106 | + } |
| 107 | + // Set private key path |
| 108 | + if (cfg.getPrivateKeyPath() != null && !cfg.getPrivateKeyPath().trim().isEmpty()) { |
| 109 | + String privateKeyPath = cfg.getPrivateKeyPath(); |
| 110 | + return new String(Files.readAllBytes(Paths.get(privateKeyPath)), StandardCharsets.UTF_8); |
| 111 | + } |
| 112 | + // Set credentials file |
| 113 | + if (cfg.getCredentialsFilePath() != null && !cfg.getCredentialsFilePath().trim().isEmpty()) { |
| 114 | + return readValueFromCredentialsFile(cfg.getCredentialsFilePath(), EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY, EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY_PATH); |
| 115 | + } |
| 116 | + |
| 117 | + // ENVs config |
| 118 | + if (EnvironmentVariables.STACKIT_PRIVATE_KEY != null && !EnvironmentVariables.STACKIT_PRIVATE_KEY.trim().isEmpty()) { |
| 119 | + return EnvironmentVariables.STACKIT_PRIVATE_KEY.trim(); |
| 120 | + } |
| 121 | + if (EnvironmentVariables.STACKIT_PRIVATE_KEY_PATH != null && !EnvironmentVariables.STACKIT_PRIVATE_KEY_PATH.trim().isEmpty()) { |
| 122 | + return new String(Files.readAllBytes(Paths.get(EnvironmentVariables.STACKIT_PRIVATE_KEY_PATH)), StandardCharsets.UTF_8); |
| 123 | + } |
| 124 | + if (EnvironmentVariables.STACKIT_CREDENTIALS_PATH != null && !EnvironmentVariables.STACKIT_CREDENTIALS_PATH.trim().isEmpty()) { |
| 125 | + return readValueFromCredentialsFile(EnvironmentVariables.STACKIT_CREDENTIALS_PATH, EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY, EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY_PATH); |
| 126 | + } |
| 127 | + |
| 128 | + // Read from credentials file in defaultCredentialsFilePath |
| 129 | + return readValueFromCredentialsFile(defaultCredentialsFilePath, EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY, EnvironmentVariables.ENV_STACKIT_PRIVATE_KEY_PATH); |
| 130 | + } |
| 131 | + |
| 132 | + private String readValueFromCredentialsFile(String path, String valueKey, String pathKey) throws Exception { |
| 133 | + // Read credentials file |
| 134 | + String fileContent = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); |
| 135 | + Type credentialsFileType = new TypeToken<Map<String, String>>(){}.getType(); |
| 136 | + Map<String, String> map = new Gson().fromJson(fileContent, credentialsFileType); |
| 137 | + |
| 138 | + // Read STACKIT_PRIVATE_KEY from credentials file |
| 139 | + String privateKey = map.get(valueKey); |
| 140 | + if (privateKey != null && !privateKey.trim().isEmpty()) { |
| 141 | + return privateKey; |
| 142 | + } |
| 143 | + |
| 144 | + // Read STACKIT_PRIVATE_KEY_PATH from credentials file |
| 145 | + String privateKeyPath = map.get(pathKey); |
| 146 | + if (privateKeyPath != null && !privateKeyPath.trim().isEmpty()) { |
| 147 | + return new String(Files.readAllBytes(Paths.get(privateKeyPath))); |
| 148 | + } |
| 149 | + throw new Exception("could not find private key"); |
| 150 | + } |
| 151 | +} |
0 commit comments