-
Notifications
You must be signed in to change notification settings - Fork 3
/
credentials.go
143 lines (110 loc) · 3.44 KB
/
credentials.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package core
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"text/template"
"github.com/awnumar/memguard"
"github.com/rs/zerolog/log"
"github.com/gookit/color"
)
type InitClientConfig struct {
ConfDir string
ClientConfig IAMClientConfig
Scanner GetInputWrapper
HTTPClient http.Client
IAMServer string
ClientTemplate string
NoPWD bool
}
func (t *InitClientConfig) InitClient(instance string) (endpoint string, clientResponse ClientResponse, passwd *memguard.Enclave, err error) {
rbody, err := ioutil.ReadFile(t.ConfDir + "/" + instance + ".json")
if err != nil {
tmpl, errParser := template.New("client").Parse(t.ClientTemplate)
if errParser != nil {
panic(errParser)
}
var b bytes.Buffer
errExecute := tmpl.Execute(&b, t.ClientConfig)
if errExecute != nil {
panic(errExecute)
}
request := b.String()
log.Info().Str("URL", request).Msg("credentials")
contentType := "application/json"
log.Info().Str("REFRESH_TOKEN", os.Getenv("REFRESH_TOKEN")).Msg("credentials")
if t.IAMServer == "" {
endpoint, err = t.Scanner.GetInputString("Insert the IAM endpoint",
"https://iam-demo.cloud.cnaf.infn.it")
if err != nil {
panic(err)
}
} else if t.IAMServer != "" {
log.Info().Str("IAM endpoint used", t.IAMServer).Msg("Credentials")
color.Green.Printf("==> IAM endpoint used: %s", t.IAMServer)
endpoint = t.IAMServer
}
register := endpoint + "/register"
log.Info().Str("IAM register url", register).Msg("Credentials")
color.Green.Printf("==> IAM register url: %s", register)
r, err := t.HTTPClient.Post(register, contentType, strings.NewReader(request))
if err != nil {
panic(err)
}
log.Info().Int("StatusCode", r.StatusCode).Str("Status", r.Status).Msg("credentials")
rbody, errReadBody := ioutil.ReadAll(r.Body)
if errReadBody != nil {
panic(errReadBody)
}
log.Info().Str("body", string(rbody)).Msg("credentials")
errUnmarshall := json.Unmarshal(rbody, &clientResponse)
if errUnmarshall != nil {
panic(errUnmarshall)
}
clientResponse.Endpoint = endpoint
if !t.NoPWD {
var errGetPasswd error
if os.Getenv("REFRESH_TOKEN") == "" {
passMsg := fmt.Sprintf("%s Insert a pasword for the secret's encryption: ", color.Yellow.Sprint("==>"))
passwd, errGetPasswd = t.Scanner.GetPassword(passMsg, false)
if errGetPasswd != nil {
panic(errGetPasswd)
}
} else {
passwd = memguard.NewEnclave([]byte("asdasdasd"))
}
dumpClient := Encrypt(rbody, passwd)
err = ioutil.WriteFile(t.ConfDir+"/"+instance+".json", dumpClient, 0600)
if err != nil {
panic(err)
}
}
} else {
if !t.NoPWD {
var errGetPasswd error
if os.Getenv("REFRESH_TOKEN") == "" {
passMsg := fmt.Sprintf("%s Insert a pasword for the secret's decryption: ", color.Yellow.Sprint("==>"))
passwd, errGetPasswd = t.Scanner.GetPassword(passMsg, true)
if errGetPasswd != nil {
panic(errGetPasswd)
}
} else {
passwd = memguard.NewEnclave([]byte("asdasdasd"))
}
errUnmarshal := json.Unmarshal(Decrypt(rbody, passwd), &clientResponse)
if errUnmarshal != nil {
panic(errUnmarshal)
}
log.Info().Str("response endpoint", clientResponse.Endpoint).Msg("credentials")
endpoint = strings.Split(clientResponse.Endpoint, "/register")[0]
}
}
if endpoint == "" {
panic("Something went wrong. No endpoint selected")
}
return endpoint, clientResponse, passwd, nil
}