1//
2// Copyright (c) 2018, Joyent, Inc. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7//
8
9package main
10
11import (
12	"context"
13	"fmt"
14	"io/ioutil"
15	"log"
16	"os"
17
18	"encoding/pem"
19
20	triton "github.com/joyent/triton-go"
21	"github.com/joyent/triton-go/account"
22	"github.com/joyent/triton-go/authentication"
23	"github.com/joyent/triton-go/network"
24)
25
26func main() {
27	keyID := os.Getenv("TRITON_KEY_ID")
28	accountName := os.Getenv("TRITON_ACCOUNT")
29	keyMaterial := os.Getenv("TRITON_KEY_MATERIAL")
30	userName := os.Getenv("TRITON_USER")
31
32	var signer authentication.Signer
33	var err error
34
35	if keyMaterial == "" {
36		input := authentication.SSHAgentSignerInput{
37			KeyID:       keyID,
38			AccountName: accountName,
39			Username:    userName,
40		}
41		signer, err = authentication.NewSSHAgentSigner(input)
42		if err != nil {
43			log.Fatalf("Error Creating SSH Agent Signer: %v", err)
44		}
45	} else {
46		var keyBytes []byte
47		if _, err = os.Stat(keyMaterial); err == nil {
48			keyBytes, err = ioutil.ReadFile(keyMaterial)
49			if err != nil {
50				log.Fatalf("Error reading key material from %s: %s",
51					keyMaterial, err)
52			}
53			block, _ := pem.Decode(keyBytes)
54			if block == nil {
55				log.Fatalf(
56					"Failed to read key material '%s': no key found", keyMaterial)
57			}
58
59			if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
60				log.Fatalf(
61					"Failed to read key '%s': password protected keys are\n"+
62						"not currently supported. Please decrypt the key prior to use.", keyMaterial)
63			}
64
65		} else {
66			keyBytes = []byte(keyMaterial)
67		}
68
69		input := authentication.PrivateKeySignerInput{
70			KeyID:              keyID,
71			PrivateKeyMaterial: keyBytes,
72			AccountName:        accountName,
73			Username:           userName,
74		}
75		signer, err = authentication.NewPrivateKeySigner(input)
76		if err != nil {
77			log.Fatalf("Error Creating SSH Private Key Signer: %v", err)
78		}
79	}
80
81	config := &triton.ClientConfig{
82		TritonURL:   os.Getenv("TRITON_URL"),
83		AccountName: accountName,
84		Username:    userName,
85		Signers:     []authentication.Signer{signer},
86	}
87
88	nc, err := network.NewClient(config)
89	if err != nil {
90		log.Fatalf("network.NewClient: %v", err)
91	}
92
93	ac, err := account.NewClient(config)
94	if err != nil {
95		log.Fatalf("account.NewClient: %v", err)
96	}
97
98	cfg, err := ac.Config().Get(context.Background(), &account.GetConfigInput{})
99	if err != nil {
100		log.Fatalf("account.Config.Get: %v", err)
101	}
102	currentNet := cfg.DefaultNetwork
103	fmt.Println("Current Network:", currentNet)
104
105	var defaultNet string
106	networks, err := nc.List(context.Background(), &network.ListInput{})
107	if err != nil {
108		log.Fatalf("network.List: %v", err)
109	}
110	for _, iterNet := range networks {
111		if iterNet.Id != currentNet {
112			defaultNet = iterNet.Id
113		}
114	}
115	fmt.Println("Chosen Network:", defaultNet)
116
117	input := &account.UpdateConfigInput{
118		DefaultNetwork: defaultNet,
119	}
120	_, err = ac.Config().Update(context.Background(), input)
121	if err != nil {
122		log.Fatalf("account.Config.Update: %v", err)
123	}
124
125	cfg, err = ac.Config().Get(context.Background(), &account.GetConfigInput{})
126	if err != nil {
127		log.Fatalf("account.Config.Get: %v", err)
128	}
129	fmt.Println("Default Network:", cfg.DefaultNetwork)
130}
131