-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(inputs.aliyuncms): Refactor common parts to plugins/common #18280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package aliyun | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth" | ||
| "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials/providers" | ||
| ) | ||
|
|
||
| // CredentialConfig holds Aliyun authentication configuration | ||
| type CredentialConfig struct { | ||
| AccessKeyID string | ||
| AccessKeySecret string | ||
| AccessKeyStsToken string | ||
| RoleArn string | ||
| RoleSessionName string | ||
| PrivateKey string | ||
| PublicKeyID string | ||
| RoleName string | ||
| } | ||
|
|
||
| // GetCredentials retrieves Aliyun credentials using the credential chain | ||
| // Credentials are loaded in the following order: | ||
| // 1) Ram RoleArn credential | ||
| // 2) AccessKey STS token credential | ||
| // 3) AccessKey credential | ||
| // 4) Ecs Ram Role credential | ||
| // 5) RSA keypair credential | ||
| // 6) Environment variables credential | ||
| // 7) Instance metadata credential | ||
| func GetCredentials(config CredentialConfig) (auth.Credential, error) { | ||
| var ( | ||
| roleSessionExpiration = 3600 | ||
| sessionExpiration = 3600 | ||
| ) | ||
|
|
||
| configuration := &providers.Configuration{ | ||
| AccessKeyID: config.AccessKeyID, | ||
| AccessKeySecret: config.AccessKeySecret, | ||
| AccessKeyStsToken: config.AccessKeyStsToken, | ||
| RoleArn: config.RoleArn, | ||
| RoleSessionName: config.RoleSessionName, | ||
| RoleSessionExpiration: &roleSessionExpiration, | ||
| PrivateKey: config.PrivateKey, | ||
| PublicKeyID: config.PublicKeyID, | ||
| SessionExpiration: &sessionExpiration, | ||
| RoleName: config.RoleName, | ||
| } | ||
|
|
||
| credentialProviders := []providers.Provider{ | ||
| providers.NewConfigurationCredentialProvider(configuration), | ||
| providers.NewEnvCredentialProvider(), | ||
| providers.NewInstanceMetadataProvider(), | ||
| } | ||
|
|
||
| credential, err := providers.NewChainProvider(credentialProviders).Retrieve() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to retrieve credential: %w", err) | ||
| } | ||
|
|
||
| return credential, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package aliyun | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetCredentials(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| config CredentialConfig | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "with access key credentials", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with access key and STS token", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| AccessKeyStsToken: "test-sts-token", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with role ARN", | ||
| config: CredentialConfig{ | ||
| AccessKeyID: "test-key-id", | ||
| AccessKeySecret: "test-key-secret", | ||
| RoleArn: "acs:ram::123456:role/test-role", | ||
| RoleSessionName: "test-session", | ||
| }, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "with RSA keypair", | ||
| config: CredentialConfig{ | ||
| PublicKeyID: "test-public-key-id", | ||
| PrivateKey: "-----BEGIN RSA PRIVATE KEY-----\ntest\n-----END RSA PRIVATE KEY-----", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "with ECS RAM role name", | ||
| config: CredentialConfig{ | ||
| RoleName: "test-ecs-role", | ||
| }, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cred, err := GetCredentials(tt.config) | ||
|
|
||
| if tt.expectError { | ||
| if err == nil { | ||
| require.NotNil(t, cred) | ||
| } | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.NotNil(t, cred) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
srebhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestGetCredentialsEmpty(t *testing.T) { | ||
| cred, err := GetCredentials(CredentialConfig{}) | ||
|
|
||
| if err != nil { | ||
| require.Contains(t, err.Error(), "failed to retrieve credential") | ||
| } else { | ||
| require.NotNil(t, cred) | ||
| } | ||
|
Comment on lines
+78
to
+82
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No! You should know what you expect from that call! Either this should return an error, then check for it, or it doesn't! |
||
| } | ||
|
|
||
| func TestCredentialConfigFields(t *testing.T) { | ||
| config := CredentialConfig{ | ||
| AccessKeyID: "key-id", | ||
| AccessKeySecret: "key-secret", | ||
| AccessKeyStsToken: "sts-token", | ||
| RoleArn: "role-arn", | ||
| RoleSessionName: "session-name", | ||
| PrivateKey: "private-key", | ||
| PublicKeyID: "public-key-id", | ||
| RoleName: "role-name", | ||
| } | ||
|
|
||
| require.Equal(t, "key-id", config.AccessKeyID) | ||
| require.Equal(t, "key-secret", config.AccessKeySecret) | ||
| require.Equal(t, "sts-token", config.AccessKeyStsToken) | ||
| require.Equal(t, "role-arn", config.RoleArn) | ||
| require.Equal(t, "session-name", config.RoleSessionName) | ||
| require.Equal(t, "private-key", config.PrivateKey) | ||
| require.Equal(t, "public-key-id", config.PublicKeyID) | ||
| require.Equal(t, "role-name", config.RoleName) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use