Skip to content

Commit

Permalink
ref(deisctl): new config logic related to filesystem and base64 opera…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Gabriel Monroy committed Jan 15, 2015
1 parent dbd42a7 commit bae0085
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 21 deletions.
58 changes: 37 additions & 21 deletions deisctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"github.com/deis/deis/deisctl/utils"
)

// fileKeys define config keys to be read from local files
var fileKeys = []string{
"/deis/platform/sshPrivateKey",
"/deis/router/sslCert",
"/deis/router/sslKey"}

// b64Keys define config keys to be base64 encoded before stored
var b64Keys = []string{"/deis/platform/sshPrivateKey"}

// Config runs the config subcommand
func Config(args map[string]interface{}) error {
return doConfig(args)
Expand Down Expand Up @@ -62,25 +71,14 @@ func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error
for _, kv := range kvs {

// split k/v from args
split := strings.Split(kv, "=")
if len(split) != 2 {
return result, fmt.Errorf("invalid argument: %v", kv)
}
split := strings.SplitN(kv, "=", 2)
k, v := split[0], split[1]

// prepare path and value
path := root + k
var val string

// special handling for sshKey
if path == "/deis/platform/sshPrivateKey" {
b64, err := readSSHPrivateKey(utils.ResolvePath(v))
if err != nil {
return result, err
}
val = b64
} else {
val = v
val, err := valueForPath(path, v)
if err != nil {
return result, err
}

// set key/value in etcd
Expand All @@ -106,13 +104,31 @@ func doConfigGet(client *etcdClient, root string, keys []string) ([]string, erro
return result, nil
}

// readSSHPrivateKey reads the key file and returns a base64 encoded string
func readSSHPrivateKey(path string) (string, error) {
// valueForPath returns the canonical value for a user-defined path and value
func valueForPath(path string, v string) (string, error) {

bytes, err := ioutil.ReadFile(path)
if err != nil {
return "", err
// check if path is part of fileKeys
for _, p := range fileKeys {

if path == p {

// read value from filesystem
bytes, err := ioutil.ReadFile(utils.ResolvePath(v))
if err != nil {
return "", err
}

// see if we should return base64 encoded value
for _, pp := range b64Keys {
if path == pp {
return base64.StdEncoding.EncodeToString(bytes), nil
}
}

return string(bytes), nil
}
}

return base64.StdEncoding.EncodeToString(bytes), nil
return v, nil

}
76 changes: 76 additions & 0 deletions deisctl/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package config

import (
"encoding/base64"
"io/ioutil"
"os"
"testing"
)

// TestConfigSSHPrivateKey ensures private keys are base64 encoded from file path
func TestConfigSSHPrivateKey(t *testing.T) {

f, err := writeTempFile("private-key")
if err != nil {
t.Fatal(err)
}

val, err := valueForPath("/deis/platform/sshPrivateKey", f.Name())
if err != nil {
t.Fatal(err)
}

encoded := base64.StdEncoding.EncodeToString([]byte("private-key"))

if val != encoded {
t.Fatalf("expected: %v, got: %v", encoded, val)
}
}

func TestConfigRouterKey(t *testing.T) {

f, err := writeTempFile("router-key")
if err != nil {
t.Fatal(err)
}

val, err := valueForPath("/deis/router/sslKey", f.Name())
if err != nil {
t.Fatal(err)
}

if val != "router-key" {
t.Fatalf("expected: router-key, got: %v", val)
}

}

func TestConfigRouterCert(t *testing.T) {

f, err := writeTempFile("router-cert")
if err != nil {
t.Fatal(err)
}

val, err := valueForPath("/deis/router/sslCert", f.Name())
if err != nil {
t.Fatal(err)
}

if val != "router-cert" {
t.Fatalf("expected: router-cert, got: %v", val)
}

}

func writeTempFile(data string) (*os.File, error) {
f, err := ioutil.TempFile("", "deisctl")
if err != nil {
return nil, err
}

f.Write([]byte(data))
defer f.Close()

return f, nil
}

0 comments on commit bae0085

Please sign in to comment.