|
| 1 | +package ibmiam |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/IBM/go-sdk-core/v5/core" |
| 5 | + "github.com/IBM/ibm-cos-sdk-go/aws" |
| 6 | + "github.com/IBM/ibm-cos-sdk-go/aws/awserr" |
| 7 | + "github.com/IBM/ibm-cos-sdk-go/aws/credentials" |
| 8 | + "github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam/token" |
| 9 | +) |
| 10 | + |
| 11 | +// Provider Struct |
| 12 | +type TrustedProfileProvider struct { |
| 13 | + // Name of Provider |
| 14 | + providerName string |
| 15 | + |
| 16 | + // Type of Provider - SharedCred, SharedConfig, etc. |
| 17 | + providerType string |
| 18 | + |
| 19 | + // Authenticator instance, will be assigned dynamically |
| 20 | + authenticator core.Authenticator |
| 21 | + |
| 22 | + // Service Instance ID passes in a provider |
| 23 | + serviceInstanceID string |
| 24 | + |
| 25 | + // Error |
| 26 | + ErrorStatus error |
| 27 | + |
| 28 | + // Logger attributes |
| 29 | + logger aws.Logger |
| 30 | + logLevel *aws.LogLevelType |
| 31 | +} |
| 32 | + |
| 33 | +// NewTrustedProfileProvider allows the creation of a custom IBM IAM Trusted Profile Provider |
| 34 | +// Parameters: |
| 35 | +// |
| 36 | +// Provider Name |
| 37 | +// AWS Config |
| 38 | +// Trusted Profile ID |
| 39 | +// CR token file path |
| 40 | +// IBM IAM Authentication Server Endpoint |
| 41 | +// Service Instance ID |
| 42 | +// Resource type |
| 43 | +// |
| 44 | +// Returns: |
| 45 | +// |
| 46 | +// TrustedProfileProvider |
| 47 | +func NewTrustedProfileProvider(providerName string, config *aws.Config, authEndPoint string, trustedProfileID string, crTokenFilePath string, |
| 48 | + serviceInstanceID string, resourceType string) (provider *TrustedProfileProvider) { |
| 49 | + provider = new(TrustedProfileProvider) |
| 50 | + |
| 51 | + provider.providerName = providerName |
| 52 | + provider.providerType = "oauth" |
| 53 | + |
| 54 | + logLevel := aws.LogLevel(aws.LogOff) |
| 55 | + if config != nil && config.LogLevel != nil && config.Logger != nil { |
| 56 | + logLevel = config.LogLevel |
| 57 | + provider.logger = config.Logger |
| 58 | + } |
| 59 | + provider.logLevel = logLevel |
| 60 | + |
| 61 | + if crTokenFilePath == "" { |
| 62 | + provider.ErrorStatus = awserr.New("crTokenFilePathNotFound", "CR Token file path not found", nil) |
| 63 | + if provider.logLevel.Matches(aws.LogDebug) { |
| 64 | + provider.logger.Log(debugLog, "<IBM IAM PROVIDER BUILD>", provider.ErrorStatus) |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + if trustedProfileID == "" { |
| 70 | + provider.ErrorStatus = awserr.New("trustedProfileIDNotFound", "Trusted profile id not found", nil) |
| 71 | + if provider.logLevel.Matches(aws.LogDebug) { |
| 72 | + provider.logger.Log(debugLog, "<IBM IAM PROVIDER BUILD>", provider.ErrorStatus) |
| 73 | + } |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + provider.serviceInstanceID = serviceInstanceID |
| 78 | + |
| 79 | + if authEndPoint == "" { |
| 80 | + authEndPoint = defaultAuthEndPoint |
| 81 | + if provider.logLevel.Matches(aws.LogDebug) { |
| 82 | + provider.logger.Log(debugLog, "<IBM IAM PROVIDER BUILD>", "using default auth endpoint", authEndPoint) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // This authenticator is dynamically initialized based on the resourceType parameter. |
| 87 | + // Since only cr-token based resources is supported now, it is initialized directly. |
| 88 | + // when other resources are supported, the authenticator should be initialized accordingly. |
| 89 | + authenticator, err := core.NewContainerAuthenticatorBuilder(). |
| 90 | + SetCRTokenFilename(crTokenFilePath). |
| 91 | + SetIAMProfileID(trustedProfileID). |
| 92 | + SetURL(authEndPoint). |
| 93 | + SetDisableSSLVerification(true). |
| 94 | + Build() |
| 95 | + if err != nil { |
| 96 | + provider.ErrorStatus = awserr.New("errCreatingAuthenticatorClient", "cannot setup new Authenticator client", err) |
| 97 | + if provider.logLevel.Matches(aws.LogDebug) { |
| 98 | + provider.logger.Log(debugLog, "<IBM IAM PROVIDER BUILD>", provider.ErrorStatus) |
| 99 | + } |
| 100 | + return |
| 101 | + } |
| 102 | + provider.authenticator = authenticator |
| 103 | + |
| 104 | + return provider |
| 105 | +} |
| 106 | + |
| 107 | +// IsValid ... |
| 108 | +// Returns: |
| 109 | +// |
| 110 | +// TrustedProfileProvider validation - boolean |
| 111 | +func (p *TrustedProfileProvider) IsValid() bool { |
| 112 | + return nil == p.ErrorStatus |
| 113 | +} |
| 114 | + |
| 115 | +// Retrieve ... |
| 116 | +// Returns: |
| 117 | +// |
| 118 | +// Credential values |
| 119 | +// Error |
| 120 | +func (p *TrustedProfileProvider) Retrieve() (credentials.Value, error) { |
| 121 | + if p.ErrorStatus != nil { |
| 122 | + if p.logLevel.Matches(aws.LogDebug) { |
| 123 | + p.logger.Log(debugLog, ibmiamProviderLog, p.providerName, p.ErrorStatus) |
| 124 | + } |
| 125 | + return credentials.Value{ProviderName: p.providerName}, p.ErrorStatus |
| 126 | + } |
| 127 | + |
| 128 | + // The respective resourceTypes's class should be called based on the resourceType parameter. |
| 129 | + // Since only cr-token based resources is supported now, it is assigned to ContainerAuthenticator |
| 130 | + // directly. when other resource types are supported, the respective class should be used accordingly. |
| 131 | + tokenValue, err := p.authenticator.(*core.ContainerAuthenticator).GetToken() |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + var returnErr error |
| 135 | + if p.logLevel.Matches(aws.LogDebug) { |
| 136 | + p.logger.Log(debugLog, ibmiamProviderLog, p.providerName, "ERROR ON GET", err) |
| 137 | + } |
| 138 | + returnErr = awserr.New("TokenManagerRetrieveError", "error retrieving the token", err) |
| 139 | + return credentials.Value{}, returnErr |
| 140 | + } |
| 141 | + |
| 142 | + return credentials.Value{ |
| 143 | + Token: token.Token{ |
| 144 | + AccessToken: tokenValue, |
| 145 | + TokenType: "Bearer", |
| 146 | + }, |
| 147 | + ProviderName: p.providerName, |
| 148 | + ProviderType: p.providerType, |
| 149 | + ServiceInstanceID: p.serviceInstanceID, |
| 150 | + }, nil |
| 151 | +} |
| 152 | + |
| 153 | +// IsExpired ... |
| 154 | +// |
| 155 | +// TrustedProfileProvider expired or not - boolean |
| 156 | +func (p *TrustedProfileProvider) IsExpired() bool { |
| 157 | + return true |
| 158 | +} |
0 commit comments