Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Adding ansible credentials and bugfix for 2.0 #327

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
36 changes: 32 additions & 4 deletions pkg/credential/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,33 @@ func (s Settings) WriteCredentialsFields(fields Fields, path string) error {
// WriteDefault is a non override version of WriteCredentialsFields
// used to create providers.json if user dont have it
func (s Settings) WriteDefaultCredentialsFields(path string) error {
if !s.file.Exists(path) {
err := s.WriteCredentialsFields(NewDefaultCredentials(), path)
return err
fieldsToWrite := NewDefaultCredentials()
if s.file.Exists(path) {
configFile, err := s.file.Read(path)
if err != nil {
return err
}

// Join saved fields and default fields
credentialFields := Fields{}
if err = json.Unmarshal(configFile, &credentialFields); err != nil {
return err
}

originalLength := len(credentialFields)
for k, v := range fieldsToWrite {
credentialFields[k] = v
}

// Avoid I/O consumption if there is nothing to change
if originalLength == len(credentialFields) {
return nil
}

fieldsToWrite = credentialFields
}
return nil
err := s.WriteCredentialsFields(fieldsToWrite, path)
return err
}

func NewDefaultCredentials() Fields {
Expand Down Expand Up @@ -163,13 +185,19 @@ func NewDefaultCredentials() Fields {
Type: "text",
}

password := Field{
Name: "password",
Type: "secret",
}

dc := Fields{
AddNew: []Field{},
"github": []Field{username, token},
"gitlab": []Field{username, token},
"aws": []Field{accessKey, secretAccessKey},
"jenkins": []Field{username, token},
"kubeconfig": []Field{base64config},
"ansible": []Field{username, password},
}

return dc
Expand Down
39 changes: 39 additions & 0 deletions pkg/credential/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package credential

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -111,6 +113,43 @@ func TestNewDefaultCredentials(t *testing.T) {
}
}

func TestSingleSettings_WriteDefaultCredentialsOnExistingFile(t *testing.T) {
credentials := Fields{
"customField": []Field{},
}
fieldsData, err := json.Marshal(credentials)
if err != nil {
t.Errorf("Error while writing existing credentials: %s", err)
}

// Write an initial credential file
err = ioutil.WriteFile(providersPath(), fieldsData, os.ModePerm)
defer os.Remove(providersPath())
if err != nil {
t.Errorf("Error while writing existing credentials: %s", err)
}

// Call the method
err = credSettings.WriteDefaultCredentialsFields(providersPath())
if err != nil {
t.Errorf("Error while writing existing credentials: %s", err)
}

// Reopen file and check if previous config was not lost
file, _ := ioutil.ReadFile(providersPath())
var fields Fields
err = json.Unmarshal(file, &fields)
if err != nil {
t.Errorf("Error while writing existing credentials: %s", err)
}
if len(fields) != len(NewDefaultCredentials()) + 1 {
t.Errorf("Writing existing credentials did not succeed in adding a field")
}
if fields["customField"] == nil {
t.Errorf("Writing existing credentials did not save custom field")
}
}

func TestProviderPath(t *testing.T) {
provider := credSettings.ProviderPath()
slicedPath := strings.Split(provider, string(os.PathSeparator))
Expand Down
10 changes: 10 additions & 0 deletions testdata/credentials.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,15 @@
"field": "base64config",
"type": "text"
}
],
"ansible": [
{
"field": "username",
"type": "text"
},
{
"field": "password",
"type": "password"
}
]
}