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

Automatically creates the token auth file if it isn't present #439

Merged
merged 2 commits into from
Mar 23, 2017
Merged
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost nit but wouldn't it be safer to close the opened file afterwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, forgot to close it. 😓

@mumoshu just to let you know, feel free not to merge my PRs unless they are 100% according to your standards regarding code style, quality, and so on; just give me your feedback and I'll try to address them ASAP.

}

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