-
Notifications
You must be signed in to change notification settings - Fork 3
/
credentials.go
181 lines (137 loc) · 4.46 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package core
import (
"bytes"
"encoding/json"
"fmt"
"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) { //nolint:funlen,cyclop,gocognit,lll
filename := t.ConfDir + "/" + instance + ".json"
log.Debug().Str("filename", filename).Msg("credentials - init client")
confFile, err := os.Open(filename)
switch {
case err != nil && err.Error() != "no such file or directory":
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.Debug().Str("URL", request).Msg("credentials")
contentType := "application/json"
log.Debug().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.Debug().Str("IAM endpoint used", t.IAMServer).Msg("credentials")
color.Green.Printf("==> IAM endpoint used: %s\n", t.IAMServer)
endpoint = t.IAMServer
}
register := endpoint + "/register"
log.Debug().Str("IAM register url", register).Msg("credentials")
color.Green.Printf("==> IAM register url: %s\n", register)
resp, err := t.HTTPClient.Post(register, contentType, strings.NewReader(request))
if err != nil {
panic(err)
}
defer resp.Body.Close()
log.Debug().Int("StatusCode", resp.StatusCode).Str("Status", resp.Status).Msg("credentials")
var rbody bytes.Buffer
_, err = rbody.ReadFrom(resp.Body)
if err != nil {
log.Err(err).Msg("credentials - read body")
panic(err)
}
log.Debug().Str("body", rbody.String()).Msg("credentials")
errUnmarshall := json.Unmarshal(rbody.Bytes(), &clientResponse)
if errUnmarshall != nil {
panic(errUnmarshall)
}
clientResponse.Endpoint = endpoint
if !t.NoPWD { //nolint:nestif
var errGetPasswd error
// TODO: verify branch when REFRESH_TOKEN is passed and is not empty string
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("nopassword"))
}
dumpClient := Encrypt(rbody.Bytes(), passwd)
filename := t.ConfDir + "/" + instance + ".json"
curFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Err(err).Msg("credentials - dump client")
panic(err)
}
_, err = curFile.Write(dumpClient)
if err != nil {
log.Err(err).Msg("credentials - dump client")
panic(err)
}
err = curFile.Close()
if err != nil {
log.Err(err).Msg("credentials - dump client")
panic(err)
}
}
case err == nil && !t.NoPWD:
var errGetPasswd error
defer confFile.Close()
var rbody bytes.Buffer
_, err = rbody.ReadFrom(confFile)
if err != nil {
log.Err(err).Msg("credentials - init client")
panic(err)
}
// TODO: verify branch when REFRESH_TOKEN is passed and is not empty string
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("nopassword"))
}
errUnmarshal := json.Unmarshal(Decrypt(rbody.Bytes(), passwd), &clientResponse)
if errUnmarshal != nil {
panic(errUnmarshal)
}
log.Debug().Str("response endpoint", clientResponse.Endpoint).Msg("credentials")
endpoint = strings.Split(clientResponse.Endpoint, "/register")[0]
default:
log.Err(err).Msg("credentials - init client")
panic(err)
}
if endpoint == "" {
panic("Something went wrong. No endpoint selected")
}
return endpoint, clientResponse, passwd, nil
}