Skip to content

Commit

Permalink
fix: allow setting explicit aws region
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Mar 15, 2024
1 parent 51012d3 commit 0fc9f15
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 10 deletions.
9 changes: 6 additions & 3 deletions client/sqs_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/rs/zerolog/log"
"github.com/soerenschneider/dyndns/conf"
"github.com/soerenschneider/dyndns/internal/common"
)

Expand All @@ -17,16 +18,18 @@ type SqsDispatch struct {
queueUrl string
}

func NewSqsDispatcher(queueUrl string, provider credentials.Provider) (*SqsDispatch, error) {
awsConf := &aws.Config{}
func NewSqsDispatcher(sqsConf conf.SqsConfig, provider credentials.Provider) (*SqsDispatch, error) {
awsConf := &aws.Config{
Region: aws.String(sqsConf.Region),
}
if provider != nil {
log.Info().Msg("Building AWS client using given credentials provider")
awsConf.Credentials = credentials.NewCredentials(provider)
}
awsSession := session.Must(session.NewSession(awsConf))

ret := &SqsDispatch{
queueUrl: queueUrl,
queueUrl: sqsConf.SqsQueue,
}
ret.client = sqs.New(awsSession)
return ret, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func buildNotifiers(config *conf.ClientConf) (map[string]client.EventDispatch, e

if len(config.SqsQueue) > 0 {
log.Info().Msg("Building AWS SQS notifier")
sqs, err := client.NewSqsDispatcher(config.SqsQueue, nil)
sqs, err := client.NewSqsDispatcher(config.SqsConfig, nil)
if err != nil {
errs = multierr.Append(errs, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func RunServer(config *conf.ServerConf) {
}

func buildSqs(config conf.ServerConf, requests chan common.UpdateRecordRequest, credProvider credentials.Provider) (*client.SqsListener, error) {
return client.NewSqsConsumer(config.SqsQueue, credProvider, requests)
return client.NewSqsConsumer(config.SqsConfig, credProvider, requests)
}

func buildMqtt(config conf.ServerConf, requests chan common.UpdateRecordRequest) ([]*mqtt.MqttBus, error) {
Expand Down
3 changes: 2 additions & 1 deletion conf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type ClientConf struct {
Once bool // this is not parsed via json, it's an cli flag

HttpDispatcherConf []HttpDispatcherConfig `yaml:"http_dispatcher" env:"HTTP_DISPATCHER_CONF"`
SqsQueue string `yaml:"sqs_queue" env:"SQS_QUEUE"`
SqsConfig `yaml:"sqs"`
MqttConfig `yaml:"mqtt"`
EmailConfig `yaml:"notifications"`
}
Expand Down Expand Up @@ -90,6 +90,7 @@ func ParseClientConfEnv(clientConf *ClientConf) error {
func getDefaultClientConfig() *ClientConf {
return &ClientConf{
MetricsListener: metrics.DefaultListener,
SqsConfig: DefaultSqsConfig(),
AddrFamilies: []string{AddrFamilyIpv4},
PreferredUrls: defaultHttpResolverUrls,
}
Expand Down
2 changes: 2 additions & 0 deletions conf/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestReadClientConfig(t *testing.T) {
KeyPairPath: "/tmp/keypair.json",
PreferredUrls: defaultHttpResolverUrls,
MetricsListener: "0.0.0.0:9191",
SqsConfig: DefaultSqsConfig(),
MqttConfig: MqttConfig{
Brokers: []string{"ssl://mqtt.eclipseprojects.io:8883"},
ClientId: "my-client-id",
Expand All @@ -41,6 +42,7 @@ func TestReadClientConfig(t *testing.T) {
KeyPairPath: "/tmp/keypair.json",
PreferredUrls: defaultHttpResolverUrls,
MetricsListener: "0.0.0.0:9191",
SqsConfig: DefaultSqsConfig(),
MqttConfig: MqttConfig{
Brokers: []string{"ssl://mqtt.eclipseprojects.io:8883"},
ClientId: "my-client-id",
Expand Down
3 changes: 2 additions & 1 deletion conf/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ServerConf struct {
KnownHosts map[string][]string `yaml:"known_hosts" env:"KNOWN_HOSTS" validate:"required"`
HostedZoneId string `yaml:"hosted_zone_id" env:"HOSTED_ZONE_ID" validate:"required"`
MetricsListener string `yaml:"metrics_listen,omitempty" validate:"omitempty,tcp_addr"`
SqsQueue string `yaml:"sqs_queue" env:"SQS_QUEUE"`
SqsConfig `yaml:"sqs"`
HttpConfig `yaml:"http"`
MqttConfig `yaml:"mqtt"`
VaultConfig `yaml:"vault"`
Expand All @@ -30,6 +30,7 @@ type ServerConf struct {
func GetDefaultServerConfig() *ServerConf {
return &ServerConf{
MetricsListener: metrics.DefaultListener,
SqsConfig: DefaultSqsConfig(),
MqttConfig: MqttConfig{
ClientId: "dyndns-server",
},
Expand Down
2 changes: 2 additions & 0 deletions conf/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestReadServerConfig(t *testing.T) {
KnownHosts: map[string][]string{
"host": []string{"key1", "key2"},
},
SqsConfig: DefaultSqsConfig(),
HostedZoneId: "hosted-zone-id-x",
MetricsListener: ":6666",
MqttConfig: MqttConfig{
Expand All @@ -47,6 +48,7 @@ func TestReadServerConfig(t *testing.T) {
KnownHosts: map[string][]string{
"host": []string{"key1", "key2"},
},
SqsConfig: DefaultSqsConfig(),
HostedZoneId: "hosted-zone-id-x",
MetricsListener: ":6666",
MqttConfig: MqttConfig{
Expand Down
12 changes: 12 additions & 0 deletions conf/sqs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package conf

type SqsConfig struct {
SqsQueue string `yaml:"sqs_queue" env:"SQS_QUEUE"`
Region string `yaml:"region" env:"SQS_REGION"`
}

func DefaultSqsConfig() SqsConfig {
return SqsConfig{
Region: "us-east-1",
}
}
9 changes: 6 additions & 3 deletions internal/events/sqs/sqs_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/rs/zerolog/log"
"github.com/soerenschneider/dyndns/conf"
"github.com/soerenschneider/dyndns/internal/common"
"github.com/soerenschneider/dyndns/internal/metrics"
"go.uber.org/multierr"
Expand All @@ -26,13 +27,13 @@ type SqsListener struct {

type SqsOpts func(consumer *SqsListener) error

func NewSqsConsumer(queueUrl string, provider credentials.Provider, reqChan chan common.UpdateRecordRequest, opts ...SqsOpts) (*SqsListener, error) {
func NewSqsConsumer(sqsConf conf.SqsConfig, provider credentials.Provider, reqChan chan common.UpdateRecordRequest, opts ...SqsOpts) (*SqsListener, error) {
if reqChan == nil {
return nil, errors.New("empty chan provided")
}

ret := &SqsListener{
queueUrl: queueUrl,
queueUrl: sqsConf.SqsQueue,
requests: reqChan,
}

Expand All @@ -47,7 +48,9 @@ func NewSqsConsumer(queueUrl string, provider credentials.Provider, reqChan chan
return nil, errs
}

awsConf := &aws.Config{}
awsConf := &aws.Config{
Region: aws.String(sqsConf.Region),
}

if provider != nil {
log.Info().Msg("Building AWS client using given credentials provider")
Expand Down

0 comments on commit 0fc9f15

Please sign in to comment.