-
Notifications
You must be signed in to change notification settings - Fork 0
BCH-1290: Implement ACM DataFetcher with paginated multi-region certi… #2
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
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
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
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 |
|---|---|---|
| @@ -1,22 +1,175 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| awsconfig "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/acm" | ||
| "github.com/hashicorp/go-hclog" | ||
| ) | ||
|
|
||
| // ACMClient is the subset of the AWS ACM API used by DataFetcher. | ||
| type ACMClient interface { | ||
| ListCertificates(ctx context.Context, params *acm.ListCertificatesInput, optFns ...func(*acm.Options)) (*acm.ListCertificatesOutput, error) | ||
| DescribeCertificate(ctx context.Context, params *acm.DescribeCertificateInput, optFns ...func(*acm.Options)) (*acm.DescribeCertificateOutput, error) | ||
| ListTagsForCertificate(ctx context.Context, params *acm.ListTagsForCertificateInput, optFns ...func(*acm.Options)) (*acm.ListTagsForCertificateOutput, error) | ||
| } | ||
|
|
||
| // DomainValidationOption holds per-domain validation details for a certificate. | ||
| type DomainValidationOption struct { | ||
| DomainName string `json:"domain_name"` | ||
| ValidationMethod string `json:"validation_method"` | ||
| } | ||
|
|
||
| // CertificateContext holds all fields required by the ACM compliance policies. | ||
| type CertificateContext struct { | ||
| Region string `json:"region"` | ||
| AccountID string `json:"account_id"` | ||
| CertificateArn string `json:"certificate_arn"` | ||
| DomainName string `json:"domain_name"` | ||
| Status string `json:"status"` | ||
| NotAfter *time.Time `json:"not_after,omitempty"` | ||
| KeyAlgorithm string `json:"key_algorithm"` | ||
| TransparencyLoggingPreference string `json:"transparency_logging_preference"` | ||
| DomainValidationOptions []DomainValidationOption `json:"domain_validation_options"` | ||
| InUseBy []string `json:"in_use_by"` | ||
| Tags map[string]string `json:"tags"` | ||
| } | ||
|
|
||
| // DataFetcher retrieves ACM certificate data across configured regions. | ||
| type DataFetcher struct { | ||
| logger hclog.Logger | ||
| config *PluginConfig | ||
| logger hclog.Logger | ||
| config *PluginConfig | ||
| newClient func(ctx context.Context, region string) (ACMClient, error) | ||
| } | ||
|
|
||
| func NewDataFetcher(logger hclog.Logger, config *PluginConfig) *DataFetcher { | ||
| // NewDataFetcher returns a DataFetcher using the standard AWS credential chain. | ||
| // AWS_ENDPOINT_URL is honoured automatically by the SDK for LocalStack compatibility. | ||
| func NewDataFetcher(logger hclog.Logger, cfg *PluginConfig) *DataFetcher { | ||
| return &DataFetcher{ | ||
| logger: logger, | ||
| config: config, | ||
| config: cfg, | ||
| newClient: func(ctx context.Context, region string) (ACMClient, error) { | ||
| awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return acm.NewFromConfig(awsCfg), nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // FetchData retrieves all ACM certificates across every configured region. | ||
| func (df *DataFetcher) FetchData(ctx context.Context) ([]CertificateContext, error) { | ||
| var all []CertificateContext | ||
| for _, region := range df.config.Regions { | ||
| certs, err := df.fetchRegion(ctx, region) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("region %s: %w", region, err) | ||
| } | ||
| all = append(all, certs...) | ||
| } | ||
| return all, nil | ||
| } | ||
|
|
||
| func (df *DataFetcher) fetchRegion(ctx context.Context, region string) ([]CertificateContext, error) { | ||
| client, err := df.newClient(ctx, region) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("create ACM client: %w", err) | ||
| } | ||
|
|
||
| var certs []CertificateContext | ||
| var nextToken *string | ||
| for { | ||
| out, err := client.ListCertificates(ctx, &acm.ListCertificatesInput{NextToken: nextToken}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("ListCertificates: %w", err) | ||
| } | ||
| for _, summary := range out.CertificateSummaryList { | ||
| arn := aws.ToString(summary.CertificateArn) | ||
| if arn == "" { | ||
| continue | ||
| } | ||
| cert, err := df.fetchCertificate(ctx, client, region, arn) | ||
| if err != nil { | ||
| df.logger.Warn("skipping certificate", "arn", arn, "error", err) | ||
| continue | ||
| } | ||
| certs = append(certs, cert) | ||
| } | ||
| if out.NextToken == nil { | ||
| break | ||
| } | ||
| nextToken = out.NextToken | ||
| } | ||
| return certs, nil | ||
| } | ||
|
|
||
| // FetchData retrieves ACM certificate data. Full implementation in task 005. | ||
| func (df *DataFetcher) FetchData() (map[string]any, error) { | ||
| return map[string]any{}, nil | ||
| func (df *DataFetcher) fetchCertificate(ctx context.Context, client ACMClient, region, arn string) (CertificateContext, error) { | ||
| descOut, err := client.DescribeCertificate(ctx, &acm.DescribeCertificateInput{ | ||
| CertificateArn: aws.String(arn), | ||
| }) | ||
| if err != nil { | ||
| return CertificateContext{}, fmt.Errorf("DescribeCertificate: %w", err) | ||
| } | ||
|
|
||
| tagsOut, err := client.ListTagsForCertificate(ctx, &acm.ListTagsForCertificateInput{ | ||
| CertificateArn: aws.String(arn), | ||
| }) | ||
| if err != nil { | ||
| return CertificateContext{}, fmt.Errorf("ListTagsForCertificate: %w", err) | ||
| } | ||
|
|
||
| detail := descOut.Certificate | ||
|
|
||
| transparencyPref := "" | ||
| if detail.Options != nil { | ||
| transparencyPref = string(detail.Options.CertificateTransparencyLoggingPreference) | ||
| } | ||
|
|
||
| dvos := make([]DomainValidationOption, 0, len(detail.DomainValidationOptions)) | ||
| for _, dvo := range detail.DomainValidationOptions { | ||
| dvos = append(dvos, DomainValidationOption{ | ||
| DomainName: aws.ToString(dvo.DomainName), | ||
| ValidationMethod: string(dvo.ValidationMethod), | ||
| }) | ||
| } | ||
|
|
||
| tags := make(map[string]string, len(tagsOut.Tags)) | ||
| for _, tag := range tagsOut.Tags { | ||
| tags[aws.ToString(tag.Key)] = aws.ToString(tag.Value) | ||
| } | ||
|
|
||
| inUseBy := detail.InUseBy | ||
| if inUseBy == nil { | ||
| inUseBy = []string{} | ||
| } | ||
|
|
||
| return CertificateContext{ | ||
| Region: region, | ||
| AccountID: arnAccountID(arn), | ||
| CertificateArn: arn, | ||
| DomainName: aws.ToString(detail.DomainName), | ||
| Status: string(detail.Status), | ||
| NotAfter: detail.NotAfter, | ||
| KeyAlgorithm: string(detail.KeyAlgorithm), | ||
| TransparencyLoggingPreference: transparencyPref, | ||
| DomainValidationOptions: dvos, | ||
| InUseBy: inUseBy, | ||
| Tags: tags, | ||
| }, nil | ||
| } | ||
|
|
||
| // arnAccountID extracts the 12-digit account ID from an ACM ARN. | ||
| // ARN format: arn:aws:acm:<region>:<account-id>:certificate/<id> | ||
| func arnAccountID(arn string) string { | ||
| parts := strings.Split(arn, ":") | ||
| if len(parts) >= 5 { | ||
| return parts[4] | ||
| } | ||
| return "" | ||
| } | ||
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.
Add nil check for
descOut.Certificate.If
DescribeCertificatereturns successfully but with a nilCertificatefield (edge case), subsequent accesses todetail.Options,detail.DomainValidationOptions,detail.InUseBy,detail.DomainName,detail.Status,detail.NotAfter, anddetail.KeyAlgorithmwill cause a nil pointer panic.🛡️ Proposed fix to guard against nil Certificate
📝 Committable suggestion
🤖 Prompt for AI Agents