File: jpc.go

package info (click to toggle)
golang-github-joyent-gocommon 0.0~git20161202.b787089-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 188 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,815 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *
 * gocommon - Go library to interact with the JoyentCloud
 *
 *
 * Copyright (c) 2016 Joyent Inc.
 *
 * Written by Daniele Stroppa <daniele.stroppa@joyent.com>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package jpc

import (
	"fmt"
	"io/ioutil"
	"os"
	"reflect"
	"runtime"

	"github.com/joyent/gosign/auth"
)

const (
	// Environment variables
	TritonAccount	= "TRITON_ACCOUNT"
	TritonKeyId	= "TRITON_KEY_ID"
	TritonUrl	= "TRITON_URL"
	SdcAccount	= "SDC_ACCOUNT"
	SdcKeyId	= "SDC_KEY_ID"
	SdcUrl		= "SDC_URL"
	MantaUser	= "MANTA_USER"
	MantaKeyId	= "MANTA_KEY_ID"
	MantaUrl	= "MANTA_URL"
)

var Locations = map[string]string{
	"us-east-1": "America/New_York",
	"us-west-1": "America/Los_Angeles",
	"us-sw-1":   "America/Los_Angeles",
	"eu-ams-1":  "Europe/Amsterdam",
}

// getConfig returns the value of the first available environment
// variable, among the given ones.
func getConfig(envVars ...string) (value string) {
	value = ""
	for _, v := range envVars {
		value = os.Getenv(v)
		if value != "" {
			break
		}
	}
	return
}

// getUserHome returns the value of HOME environment
// variable for the user environment.
func getUserHome() string {
	if runtime.GOOS == "windows" {
		return os.Getenv("APPDATA")
	} else {
		return os.Getenv("HOME")
	}
}

// credentialsFromEnv creates and initializes the credentials from the
// environment variables.
func credentialsFromEnv(key string) (*auth.Credentials, error) {
	var keyName string
	if key == "" {
		keyName = getUserHome() + "/.ssh/id_rsa"
	} else {
		keyName = key
	}
	privateKey, err := ioutil.ReadFile(keyName)
	if err != nil {
		return nil, err
	}
	authentication, err := auth.NewAuth(getConfig(TritonAccount, SdcAccount, MantaUser), string(privateKey), "rsa-sha256")
	if err != nil {
		return nil, err
	}

	return &auth.Credentials{
		UserAuthentication: authentication,
		SdcKeyId:           getConfig(TritonKeyId, SdcKeyId),
		SdcEndpoint:        auth.Endpoint{URL: getConfig(TritonUrl, SdcUrl)},
		MantaKeyId:         getConfig(MantaKeyId),
		MantaEndpoint:      auth.Endpoint{URL: getConfig(MantaUrl)},
	}, nil
}

// CompleteCredentialsFromEnv gets and verifies all the required
// authentication parameters have values in the environment.
func CompleteCredentialsFromEnv(keyName string) (cred *auth.Credentials, err error) {
	cred, err = credentialsFromEnv(keyName)
	if err != nil {
		return nil, err
	}
	v := reflect.ValueOf(cred).Elem()
	t := v.Type()
	for i := 0; i < v.NumField(); i++ {
		f := v.Field(i)
		if f.String() == "" {
			return nil, fmt.Errorf("Required environment variable not set for credentials attribute: %s", t.Field(i).Name)
		}
	}
	return cred, nil
}