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
|
package helpers
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"github.com/Azure/go-autorest/autorest/azure"
)
const (
credentialsPath = "/.azure/credentials.json"
)
// ToJSON returns the passed item as a pretty-printed JSON string. If any JSON error occurs,
// it returns the empty string.
func ToJSON(v interface{}) string {
j, _ := json.MarshalIndent(v, "", " ")
return string(j)
}
// NewServicePrincipalTokenFromCredentials creates a new ServicePrincipalToken using values of the
// passed credentials map.
func NewServicePrincipalTokenFromCredentials(c map[string]string, scope string) (*azure.ServicePrincipalToken, error) {
oauthConfig, err := azure.PublicCloud.OAuthConfigForTenant(c["tenantID"])
if err != nil {
panic(err)
}
return azure.NewServicePrincipalToken(*oauthConfig, c["clientID"], c["clientSecret"], scope)
}
// LoadCredentials reads credentials from a ~/.azure/credentials.json file. See the accompanying
// credentials_sample.json file for an example.
//
// Note: Storing crendentials in a local file must be secured and not shared. It is used here
// simply to reduce code in the examples.
func LoadCredentials() (map[string]string, error) {
u, err := user.Current()
if err != nil {
return nil, fmt.Errorf("ERROR: Unable to determine current user")
}
n := u.HomeDir + credentialsPath
f, err := os.Open(n)
if err != nil {
return nil, fmt.Errorf("ERROR: Unable to locate or open Azure credentials at %s (%v)", n, err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("ERROR: Unable to read %s (%v)", n, err)
}
c := map[string]interface{}{}
err = json.Unmarshal(b, &c)
if err != nil {
return nil, fmt.Errorf("ERROR: %s contained invalid JSON (%s)", n, err)
}
return ensureValueStrings(c), nil
}
func ensureValueStrings(mapOfInterface map[string]interface{}) map[string]string {
mapOfStrings := make(map[string]string)
for key, value := range mapOfInterface {
mapOfStrings[key] = ensureValueString(value)
}
return mapOfStrings
}
func ensureValueString(value interface{}) string {
if value == nil {
return ""
}
switch v := value.(type) {
case string:
return v
default:
return fmt.Sprintf("%v", v)
}
}
|