This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
/
auth.go
executable file
·240 lines (203 loc) · 5.87 KB
/
auth.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package cmd
/*
Copyright 2019 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
log "github.com/sirupsen/logrus"
)
const etcpath = "/etc/pgo/pgouser"
const pgoUserFileEnvVar = "PGOUSER"
const pgoUserNameEnvVar = "PGOUSERNAME"
const pgoUserPasswordEnvVar = "PGOUSERPASS"
// BasicAuthUsername and BasicAuthPassword are for BasicAuth, they are fetched from a file
var SessionCredentials msgs.BasicAuthCredentials
var caCertPool *x509.CertPool
var cert tls.Certificate
var httpclient *http.Client
var caCertPath, clientCertPath, clientKeyPath string
// StatusCheck ...
func StatusCheck(resp *http.Response) {
log.Debugf("HTTP status code is %d", resp.StatusCode)
if resp.StatusCode == 401 {
fmt.Printf("Error: Authentication Failed: %d\n", resp.StatusCode)
os.Exit(2)
} else if resp.StatusCode != 200 {
fmt.Printf("Error: Invalid Status Code: %d\n", resp.StatusCode)
os.Exit(2)
}
}
func UserHomeDir() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
return os.Getenv(env)
}
func parseCredentials(dat string) msgs.BasicAuthCredentials {
fields := strings.Split(strings.TrimSpace(dat), ":")
log.Debugf("%v", fields)
log.Debugf("username=[%s] password=[%s]", fields[0], fields[1])
creds := msgs.BasicAuthCredentials{
Username: fields[0],
Password: fields[1],
APIServerURL: APIServerURL,
}
return creds
}
func GetCredentialsFromFile() msgs.BasicAuthCredentials {
found := false
dir := UserHomeDir()
fullPath := dir + "/" + ".pgouser"
var creds msgs.BasicAuthCredentials
//look in env var for pgouser file
pgoUser := os.Getenv(pgoUserFileEnvVar)
if pgoUser != "" {
fullPath = pgoUser
log.Debugf("%s environment variable is being used at %s", pgoUserFileEnvVar, fullPath)
dat, err := ioutil.ReadFile(fullPath)
if err != nil {
fmt.Printf("Error: %s file not found", fullPath)
os.Exit(2)
}
log.Debugf("pgouser file found at %s contains %s", fullPath, string(dat))
creds = parseCredentials(string(dat))
found = true
}
//look in home directory for .pgouser file
if !found {
log.Debugf("looking in %s for credentials", fullPath)
dat, err := ioutil.ReadFile(fullPath)
if err != nil {
log.Debugf("%s not found", fullPath)
} else {
log.Debugf("%s found", fullPath)
log.Debugf("pgouser file found at %s contains %s", fullPath, string(dat))
creds = parseCredentials(string(dat))
found = true
}
}
//look in etc for pgouser file
if !found {
fullPath = etcpath
dat, err := ioutil.ReadFile(fullPath)
if err != nil {
log.Debugf("%s not found", etcpath)
} else {
log.Debugf("%s found", fullPath)
log.Debugf("pgouser file found at %s contains %s", fullPath, string(dat))
creds = parseCredentials(string(dat))
found = true
}
}
if !found {
fmt.Println("could not find pgouser file")
os.Exit(2)
}
return creds
}
func GetCredentialsFromEnvironment() msgs.BasicAuthCredentials {
pgoUser := os.Getenv(pgoUserNameEnvVar)
pgoPass := os.Getenv(pgoUserPasswordEnvVar)
if len(pgoUser) > 0 && len(pgoPass) < 1 {
fmt.Println("Error: PGOUSERPASS needs to be specified if PGOUSERNAME is provided")
os.Exit(2)
}
if len(pgoPass) > 0 && len(pgoUser) < 1 {
fmt.Println("Error: PGOUSERNAME needs to be specified if PGOUSERPASS is provided")
os.Exit(2)
}
creds := msgs.BasicAuthCredentials{
Username: os.Getenv(pgoUserNameEnvVar),
Password: os.Getenv(pgoUserPasswordEnvVar),
APIServerURL: APIServerURL,
}
return creds
}
func GetCredentials() {
log.Debug("GetCredentials called")
SessionCredentials = GetCredentialsFromEnvironment()
if !SessionCredentials.HasUsernameAndPassword() {
SessionCredentials = GetCredentialsFromFile()
}
if PGO_CA_CERT != "" {
caCertPath = PGO_CA_CERT
} else {
caCertPath = os.Getenv("PGO_CA_CERT")
}
if caCertPath == "" {
fmt.Println("Error: PGO_CA_CERT not specified")
os.Exit(2)
}
caCert, err := ioutil.ReadFile(caCertPath)
if err != nil {
fmt.Printf("Error: %s", err)
fmt.Println("could not read ca certificate")
os.Exit(2)
}
caCertPool = x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
if PGO_CLIENT_CERT != "" {
clientCertPath = PGO_CLIENT_CERT
} else {
clientCertPath = os.Getenv("PGO_CLIENT_CERT")
}
if clientCertPath == "" {
fmt.Println("Error: PGO_CLIENT_CERT not specified")
os.Exit(2)
}
_, err = ioutil.ReadFile(clientCertPath)
if err != nil {
log.Debugf("%s not found", clientCertPath)
os.Exit(2)
}
if PGO_CLIENT_KEY != "" {
clientKeyPath = PGO_CLIENT_KEY
} else {
clientKeyPath = os.Getenv("PGO_CLIENT_KEY")
}
if clientKeyPath == "" {
fmt.Println("Error: PGO_CLIENT_KEY not specified")
os.Exit(2)
}
_, err = ioutil.ReadFile(clientKeyPath)
if err != nil {
log.Debugf("%s not found", clientKeyPath)
os.Exit(2)
}
cert, err = tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
if err != nil {
fmt.Printf("Error loading client certificate/key: %s\n", err)
os.Exit(2)
}
log.Debug("setting up httpclient with TLS")
httpclient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: true,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS11,
},
},
}
}