|
| 1 | +/* |
| 2 | + ** Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 | + ** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 4 | + */ |
| 5 | + |
| 6 | +package com.oracle.cloud.spring.autoconfigure.core; |
| 7 | + |
| 8 | +import com.oracle.bmc.ClientRuntime; |
| 9 | +import com.oracle.bmc.Region; |
| 10 | +import com.oracle.bmc.auth.*; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Provider to wrap AuthenticationDetailsProvider for beans initialization |
| 16 | + */ |
| 17 | +public class CredentialsProvider { |
| 18 | + |
| 19 | + private static final String PROFILE_DEFAULT = "DEFAULT"; |
| 20 | + public static final String USER_AGENT_SPRING_CLOUD = "Oracle-SpringCloud"; |
| 21 | + |
| 22 | + private BasicAuthenticationDetailsProvider authenticationDetailsProvider; |
| 23 | + |
| 24 | + public CredentialsProvider(CredentialsProperties properties) throws IOException { |
| 25 | + this.authenticationDetailsProvider = createCredentialsProvider(properties); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Initializes an Authentication provider based on {@link CredentialsProperties.ConfigType} type |
| 30 | + * @return BasicAuthenticationDetailsProvider |
| 31 | + * @throws IOException |
| 32 | + */ |
| 33 | + private static BasicAuthenticationDetailsProvider createCredentialsProvider(CredentialsProperties properties) |
| 34 | + throws IOException { |
| 35 | + BasicAuthenticationDetailsProvider authenticationDetailsProvider; |
| 36 | + |
| 37 | + switch (properties.getType()) { |
| 38 | + case RESOURCE_PRINCIPAL: |
| 39 | + authenticationDetailsProvider = ResourcePrincipalAuthenticationDetailsProvider.builder().build(); |
| 40 | + break; |
| 41 | + case INSTANCE_PRINCIPAL: |
| 42 | + authenticationDetailsProvider = InstancePrincipalsAuthenticationDetailsProvider.builder().build(); |
| 43 | + break; |
| 44 | + case SIMPLE: |
| 45 | + SimpleAuthenticationDetailsProvider.SimpleAuthenticationDetailsProviderBuilder builder = SimpleAuthenticationDetailsProvider.builder() |
| 46 | + .userId(properties.getUserId()) |
| 47 | + .tenantId(properties.getTenantId()) |
| 48 | + .fingerprint(properties.getFingerprint()) |
| 49 | + .privateKeySupplier(new SimplePrivateKeySupplier(properties.getPrivateKey())) |
| 50 | + .passPhrase(properties.getPassPhrase()); |
| 51 | + if (properties.getRegion() != null) { |
| 52 | + builder.region(Region.valueOf(properties.getRegion())); |
| 53 | + } |
| 54 | + authenticationDetailsProvider = builder.build(); |
| 55 | + break; |
| 56 | + case SESSION_TOKEN: |
| 57 | + String configProfile = properties.hasProfile() ? properties.getProfile() : PROFILE_DEFAULT; |
| 58 | + |
| 59 | + if (properties.hasFile()) { |
| 60 | + authenticationDetailsProvider = new SessionTokenAuthenticationDetailsProvider(properties.getFile(), configProfile); |
| 61 | + } else { |
| 62 | + authenticationDetailsProvider = new SessionTokenAuthenticationDetailsProvider(configProfile); |
| 63 | + } |
| 64 | + break; |
| 65 | + default: |
| 66 | + String profile = properties.hasProfile() ? properties.getProfile() : PROFILE_DEFAULT; |
| 67 | + |
| 68 | + if (properties.hasFile()) { |
| 69 | + authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider(properties.getFile(), profile); |
| 70 | + } else { |
| 71 | + authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider(profile); |
| 72 | + } |
| 73 | + |
| 74 | + break; |
| 75 | + } |
| 76 | + ClientRuntime.setClientUserAgent(USER_AGENT_SPRING_CLOUD); |
| 77 | + return authenticationDetailsProvider; |
| 78 | + } |
| 79 | + |
| 80 | + public BasicAuthenticationDetailsProvider getAuthenticationDetailsProvider() { |
| 81 | + return authenticationDetailsProvider; |
| 82 | + } |
| 83 | +} |
0 commit comments