Skip to content
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

allow you to specify dynamodb endpoint and support the aws credentials profile. #777

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ before_install:
- sudo mv vault /bin/
- vault server -dev &
# Install zookeeper
- wget http://www.eu.apache.org/dist/zookeeper/zookeeper-${ZOOKEEPER_VERSION}/zookeeper-${ZOOKEEPER_VERSION}.tar.gz
- wget https://archive.apache.org/dist/zookeeper/zookeeper-${ZOOKEEPER_VERSION}/zookeeper-${ZOOKEEPER_VERSION}.tar.gz
- tar xzf zookeeper-${ZOOKEEPER_VERSION}.tar.gz
- mkdir /tmp/zookeeper && cp integration/zookeeper/zoo.cfg zookeeper-${ZOOKEEPER_VERSION}/conf/zoo.cfg
- zookeeper-${ZOOKEEPER_VERSION}/bin/zkServer.sh start
Expand Down
9 changes: 7 additions & 2 deletions backends/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package backends

import (
"errors"
"github.com/xdhuxc/confd/backends/dynamodb"
"strings"

"github.com/kelseyhightower/confd/backends/consul"
"github.com/kelseyhightower/confd/backends/dynamodb"
"github.com/kelseyhightower/confd/backends/env"
"github.com/kelseyhightower/confd/backends/etcd"
"github.com/kelseyhightower/confd/backends/etcdv3"
Expand Down Expand Up @@ -82,7 +82,12 @@ func New(config Config) (StoreClient, error) {
case "dynamodb":
table := config.Table
log.Info("DynamoDB table set to " + table)
return dynamodb.NewDynamoDBClient(table)
if len(backendNodes) >= 1 {
return dynamodb.NewDynamoDBClient(backendNodes[0], table, config.Profile)
} else {
return dynamodb.NewDynamoDBClient("", table, config.Profile)
}

case "ssm":
return ssm.New()
}
Expand Down
45 changes: 23 additions & 22 deletions backends/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import (
)

type Config struct {
AuthToken string `toml:"auth_token"`
AuthType string `toml:"auth_type"`
Backend string `toml:"backend"`
BasicAuth bool `toml:"basic_auth"`
ClientCaKeys string `toml:"client_cakeys"`
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientInsecure bool `toml:"client_insecure"`
BackendNodes util.Nodes `toml:"nodes"`
Password string `toml:"password"`
Scheme string `toml:"scheme"`
Table string `toml:"table"`
Separator string `toml:"separator"`
Username string `toml:"username"`
AppID string `toml:"app_id"`
UserID string `toml:"user_id"`
RoleID string `toml:"role_id"`
SecretID string `toml:"secret_id"`
YAMLFile util.Nodes `toml:"file"`
Filter string `toml:"filter"`
Path string `toml:"path"`
Role string
AuthToken string `toml:"auth_token"`
AuthType string `toml:"auth_type"`
Backend string `toml:"backend"`
BasicAuth bool `toml:"basic_auth"`
ClientCaKeys string `toml:"client_cakeys"`
ClientCert string `toml:"client_cert"`
ClientKey string `toml:"client_key"`
ClientInsecure bool `toml:"client_insecure"`
BackendNodes util.Nodes `toml:"nodes"`
Password string `toml:"password"`
Scheme string `toml:"scheme"`
Table string `toml:"table"`
Separator string `toml:"separator"`
Username string `toml:"username"`
AppID string `toml:"app_id"`
UserID string `toml:"user_id"`
RoleID string `toml:"role_id"`
SecretID string `toml:"secret_id"`
YAMLFile util.Nodes `toml:"file"`
Filter string `toml:"filter"`
Path string `toml:"path"`
Profile string `toml:"profile"`
Role string
}
86 changes: 71 additions & 15 deletions backends/dynamodb/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dynamodb

import (
"fmt"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"os"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,30 +22,83 @@ type Client struct {
// NewDynamoDBClient returns an *dynamodb.Client with a connection to the region
// configured via the AWS_REGION environment variable.
// It returns an error if the connection cannot be made or the table does not exist.
func NewDynamoDBClient(table string) (*Client, error) {
func NewDynamoDBClient(endpoint string, table string, profile string) (*Client, error) {
var c *aws.Config
if os.Getenv("DYNAMODB_LOCAL") != "" {
log.Debug("DYNAMODB_LOCAL is set")
endpoint := "http://localhost:8000"
c = &aws.Config{
Endpoint: &endpoint,
var creds *credentials.Credentials
var sess *session.Session
region := os.Getenv("AWS_REGION")
if region == "" {
sess, err := session.NewSession()
if err != nil {
return nil, err
}
} else {
c = nil
metadata := ec2metadata.New(sess)
tempRegion, err := metadata.Region()
if err != nil {
return nil, fmt.Errorf("the dynamodb client requires a region")
}
region = tempRegion
}

session := session.New(c)
if profile != "" {
creds = credentials.NewSharedCredentials("", profile)
if os.Getenv("DYNAMODB_LOCAL") != "" {
log.Debug("DYNAMODB_LOCAL is set")
endpoint := "http://localhost:8000"
c = &aws.Config{
Region: aws.String(region),
Endpoint: &endpoint,
Credentials: creds,
}
} else if endpoint != "" {
c = &aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
Credentials: creds,
}
} else {
c = &aws.Config{
Region: aws.String(region),
Credentials: creds,
}
}
sess = session.New(c)
// Fail early, if no credentials can be found
/*
_, err := sess.Config.Credentials.Get()
if err != nil {
return nil, err
}
*/
} else {
if os.Getenv("DYNAMODB_LOCAL") != "" {
log.Debug("DYNAMODB_LOCAL is set")
endpoint := "http://localhost:8000"
c = &aws.Config{
Endpoint: &endpoint,
}
} else if endpoint != "" {
c = &aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
}
} else {
c = nil
}

sess = session.New(c)

// Fail early, if no credentials can be found
_, err := session.Config.Credentials.Get()
if err != nil {
return nil, err
// Fail early, if no credentials can be found
_, err := sess.Config.Credentials.Get()
if err != nil {
return nil, err
}
}

d := dynamodb.New(session)
d := dynamodb.New(sess)

// Check if the table exists
_, err = d.DescribeTable(&dynamodb.DescribeTableInput{TableName: &table})
_, err := d.DescribeTable(&dynamodb.DescribeTableInput{TableName: &table})
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion confd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"runtime"
"syscall"

"github.com/kelseyhightower/confd/backends"
"github.com/kelseyhightower/confd/log"
"github.com/kelseyhightower/confd/resource/template"

"github.com/xdhuxc/confd/backends"
)

func main() {
Expand Down
6 changes: 4 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/kelseyhightower/confd/backends"
"github.com/kelseyhightower/confd/log"
"github.com/kelseyhightower/confd/resource/template"

"github.com/xdhuxc/confd/backends"
)

type TemplateConfig = template.Config
Expand Down Expand Up @@ -44,7 +45,7 @@ func init() {
flag.StringVar(&config.ClientCaKeys, "client-ca-keys", "", "client ca keys")
flag.StringVar(&config.ClientCert, "client-cert", "", "the client cert")
flag.StringVar(&config.ClientKey, "client-key", "", "the client key")
flag.BoolVar(&config.ClientInsecure, "client-insecure", false, "Allow connections to SSL sites without certs (only used with -backend=etcd)")
flag.BoolVar(&config.ClientInsecure, "client-insecure", false, "Allow connections to SSL sites without certs (only used with -backend=etcd)")
flag.StringVar(&config.ConfDir, "confdir", "/etc/confd", "confd conf directory")
flag.StringVar(&config.ConfigFile, "config-file", "/etc/confd/confd.toml", "the confd config file")
flag.Var(&config.YAMLFile, "file", "the YAML file to watch for changes (only used with -backend=file)")
Expand Down Expand Up @@ -73,6 +74,7 @@ func init() {
flag.StringVar(&config.Username, "username", "", "the username to authenticate as (only used with vault and etcd backends)")
flag.StringVar(&config.Password, "password", "", "the password to authenticate with (only used with vault and etcd backends)")
flag.BoolVar(&config.Watch, "watch", false, "enable watch support")
flag.StringVar(&config.Profile, "profile", "", "the profile for the AWS's credentials, the default is empty")
}

// initConfig initializes the confd configuration by first setting defaults,
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

const Version = "0.17.0-dev"
const Version = "0.17.1-dev"

// We want to replace this variable at build time with "-ldflags -X main.GitSHA=xxx", where const is not supported.
var GitSHA = ""