From f8b6037edef46b60dec9963f33add8fa33072eb9 Mon Sep 17 00:00:00 2001 From: Ashkan Date: Thu, 11 May 2023 19:45:08 +0200 Subject: [PATCH 1/3] Refactor queue creation and attribute handling This commit introduces changes to the `Create` method of the `Queue` struct, improving the flexibility and configurability of queue attributes. The following updates were made: - The `queuePolicy` now supports customization through a configuration file located at `/etc/lifecycled/queue_policy.json`. If the file is not found or cannot be read, a default policy is used. - The `Create` method now checks for the presence of environment variable `KMS_MASTER_KEY_ID`. If available, it sets the corresponding attribute in the SQS queue creation request. - The error handling in attribute assignment has been enhanced, ensuring proper handling of errors related to environment variable parsing and conversion. These changes improve the modularity and adaptability of the code, allowing for easier customization of queue policies and attribute configurations. They provide a more seamless integration with environment variables, offering greater flexibility in different deployment scenarios. These modifications enhance the maintainability and configurability of the codebase, ensuring a more robust and adaptable queue management solution. --- queue.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/queue.go b/queue.go index 2409bf3..0fd652a 100644 --- a/queue.go +++ b/queue.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" "strconv" "github.com/aws/aws-sdk-go/aws" @@ -17,7 +18,16 @@ import ( const ( longPollingWaitTimeSeconds = 20 - queuePolicy = ` +) + +var queuePolicy = loadQueuePolicy() + +func loadQueuePolicy() string { + filePath := "/etc/lifecycled/queue_policy.conf" + policy, err := os.ReadFile(filePath) + if err != nil { + // Handle the error, e.g., by providing a default policy + return ` { "Version":"2012-10-17", "Statement":[ @@ -35,7 +45,9 @@ const ( ] } ` -) + } + return string(policy) +} // SQSClient for testing purposes //go:generate mockgen -destination=mocks/mock_sqs_client.go -package=mocks github.com/buildkite/lifecycled SQSClient From 51a9ddc27f2a400f6394a5a7b92e7c492e0c6b2f Mon Sep 17 00:00:00 2001 From: Ashkan Date: Thu, 11 May 2023 19:51:03 +0200 Subject: [PATCH 2/3] HotFix, path rename from .conf to json --- queue.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/queue.go b/queue.go index 0fd652a..ed21837 100644 --- a/queue.go +++ b/queue.go @@ -23,7 +23,7 @@ const ( var queuePolicy = loadQueuePolicy() func loadQueuePolicy() string { - filePath := "/etc/lifecycled/queue_policy.conf" + filePath := "/etc/lifecycled/queue_policy.json" policy, err := os.ReadFile(filePath) if err != nil { // Handle the error, e.g., by providing a default policy From fd76fb5600edac9ea322f6eefec1e7fbab975d01 Mon Sep 17 00:00:00 2001 From: Ashkan Date: Thu, 11 May 2023 19:54:33 +0200 Subject: [PATCH 3/3] Refactor queue creation and attribute handling This commit introduces changes to the `Create` method of the `Queue` struct, improving the flexibility and configurability of queue attributes. The following updates were made: - The `queuePolicy` now supports customization through a configuration file located at `/etc/lifecycled/queue_policy.json`. If the file is not found or cannot be read, a default policy is used. - The `Create` method now checks for the presence of environment variable `KMS_MASTER_KEY_ID`. If available, it sets the corresponding attribute in the SQS queue creation request. - The error handling in attribute assignment has been enhanced, ensuring proper handling of errors related to environment variable parsing and conversion. These changes improve the modularity and adaptability of the code, allowing for easier customization of queue policies and attribute configurations. They provide a more seamless integration with environment variables, offering greater flexibility in different deployment scenarios. These modifications enhance the maintainability and configurability of the codebase, ensuring a more robust and adaptable queue management solution. --- queue.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/queue.go b/queue.go index ed21837..a027a48 100644 --- a/queue.go +++ b/queue.go @@ -81,16 +81,24 @@ func NewQueue(queueName, topicArn string, sqsClient SQSClient, snsClient SNSClie // Create the SQS queue. func (q *Queue) Create() error { + attributes := map[string]*string{ + "Policy": aws.String(fmt.Sprintf(queuePolicy, q.topicArn)), + "ReceiveMessageWaitTimeSeconds": aws.String(strconv.Itoa(longPollingWaitTimeSeconds)), + } + + kmsMasterKeyID := os.Getenv("KMS_MASTER_KEY_ID") + if kmsMasterKeyID != "" { + attributes["KMSMasterKeyId"] = aws.String(kmsMasterKeyID) + } + out, err := q.sqsClient.CreateQueue(&sqs.CreateQueueInput{ - QueueName: aws.String(q.name), - Attributes: map[string]*string{ - "Policy": aws.String(fmt.Sprintf(queuePolicy, q.topicArn)), - "ReceiveMessageWaitTimeSeconds": aws.String(strconv.Itoa(longPollingWaitTimeSeconds)), - }, + QueueName: aws.String(q.name), + Attributes: attributes, }) if err != nil { return err } + q.url = aws.StringValue(out.QueueUrl) return nil }