Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #439 from danielfm/auto-creates-token-file
Browse files Browse the repository at this point in the history
Automatically creates the token auth file if it isn't present
  • Loading branch information
mumoshu authored Mar 23, 2017
2 parents 63f0aba + 50b2b2c commit 73481f6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/controlplane/config/token_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/csv"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -152,6 +153,12 @@ func (r *EncryptedAuthTokensOnDisk) Compact() (*CompactAuthTokens, error) {

func ReadOrEncryptAuthTokens(dirname string, encryptor CachedEncryptor) (*EncryptedAuthTokensOnDisk, error) {
authTokenPath := filepath.Join(dirname, "tokens.csv")

// Auto-creates the auth token file, useful for those coming from previous versions of kube-aws
if _, err := os.Stat(authTokenPath); os.IsNotExist(err) {
os.OpenFile(authTokenPath, os.O_RDONLY|os.O_CREATE, 0600)
}

if _, err := ReadRawAuthTokens(dirname); err != nil {
return nil, err
}
Expand Down
25 changes: 25 additions & 0 deletions core/controlplane/config/token_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ func TestReadOrCreateCompactEmptyAuthTokens(t *testing.T) {
})
}

func TestReadOrCreateCompactNonExistentAuthTokens(t *testing.T) {
helper.WithDummyCredentials(func(dir string) {
kmsConfig := KMSConfig{
KMSKeyARN: "keyarn",
Region: model.RegionForName("us-west-1"),
EncryptService: &dummyEncryptService{},
}

if err := os.Remove(filepath.Join(dir, "tokens.csv")); err != nil {
t.Errorf("failed to remove tokens.csv for test setup : %v", err)
t.FailNow()
}

created, err := ReadOrCreateCompactAuthTokens(dir, kmsConfig)

if err != nil {
t.Errorf("failed to read or update compact auth tokens in %s : %v", dir, err)
}

if len(created.Contents) > 0 {
t.Errorf("compacted auth tokens expected to be an empty string, but was %s", created.Contents)
}
})
}

func TestReadOrCreateEmptyUnEcryptedCompactAuthTokens(t *testing.T) {
helper.WithDummyCredentials(func(dir string) {
t.Run("CachedToPreventUnnecessaryNodeReplacementOnUnencrypted", func(t *testing.T) {
Expand Down

0 comments on commit 73481f6

Please sign in to comment.