@@ -34,6 +34,8 @@ var BasicAuthUsername, BasicAuthPassword string
3434
3535var caCertPool * x509.CertPool
3636var cert tls.Certificate
37+ var httpclient * http.Client
38+ var caCertPath , clientCertPath , clientKeyPath string
3739
3840// StatusCheck ...
3941func StatusCheck (resp * http.Response ) {
@@ -72,45 +74,57 @@ func GetCredentials() {
7274 fullPath := dir + "/" + ".pgouser"
7375 log .Debug ("looking in " + fullPath + " for credentials" )
7476 dat , err := ioutil .ReadFile (fullPath )
77+ found := false
7578 if err != nil {
7679 log .Debug (fullPath + " not found" )
7780 } else {
7881 log .Debug (fullPath + " found" )
7982 log .Debug ("pgouser file found at " + fullPath + "contains " + string (dat ))
8083 BasicAuthUsername , BasicAuthPassword = parseCredentials (string (dat ))
81- return
84+ found = true
85+
8286 }
8387
84- fullPath = etcpath
85- dat , err = ioutil .ReadFile (fullPath )
86- if err != nil {
87- log .Debug (etcpath + " not found" )
88- } else {
89- log .Debug (fullPath + " found" )
88+ if ! found {
89+ fullPath = etcpath
90+ dat , err = ioutil .ReadFile (fullPath )
91+ if err != nil {
92+ log .Debug (etcpath + " not found" )
93+ } else {
94+ log .Debug (fullPath + " found" )
95+ log .Debug ("pgouser file found at " + fullPath + "contains " + string (dat ))
96+ BasicAuthUsername , BasicAuthPassword = parseCredentials (string (dat ))
97+ found = true
98+ }
99+ }
100+
101+ if ! found {
102+ pgoUser := os .Getenv (pgouserenvvar )
103+ if pgoUser == "" {
104+ log .Error (pgouserenvvar + " env var not set" )
105+ os .Exit (2 )
106+ }
107+
108+ fullPath = pgoUser
109+ log .Debug (pgouserenvvar + " env var is being used at " + fullPath )
110+ dat , err = ioutil .ReadFile (fullPath )
111+ if err != nil {
112+ log .Error (fullPath + " file not found" )
113+ os .Exit (2 )
114+ }
115+
90116 log .Debug ("pgouser file found at " + fullPath + "contains " + string (dat ))
91117 BasicAuthUsername , BasicAuthPassword = parseCredentials (string (dat ))
92- return
93118 }
94119
95- pgoUser := os .Getenv (pgouserenvvar )
96- if pgoUser == "" {
97- log .Error (pgouserenvvar + " env var not set" )
98- os .Exit (2 )
99- }
120+ caCertPath = os .Getenv ("PGO_CA_CERT" )
100121
101- fullPath = pgoUser
102- log .Debug (pgouserenvvar + " env var is being used at " + fullPath )
103- dat , err = ioutil .ReadFile (fullPath )
104- if err != nil {
105- log .Error (fullPath + " file not found" )
122+ if caCertPath == "" {
123+ log .Error ("PGO_CA_CERT not specified" )
106124 os .Exit (2 )
107125 }
108-
109- log .Debug ("pgouser file found at " + fullPath + "contains " + string (dat ))
110- BasicAuthUsername , BasicAuthPassword = parseCredentials (string (dat ))
111-
112- /**
113- caCert, err := ioutil.ReadFile("/tmp/server.crt")
126+ //caCert, err := ioutil.ReadFile("/tmp/server.crt")
127+ caCert , err := ioutil .ReadFile (caCertPath )
114128 if err != nil {
115129 log .Error (err )
116130 log .Error ("could not read ca certificate" )
@@ -119,11 +133,48 @@ func GetCredentials() {
119133 caCertPool = x509 .NewCertPool ()
120134 caCertPool .AppendCertsFromPEM (caCert )
121135
122- cert, err = tls.LoadX509KeyPair("/tmp/client.crt", "/tmp/client.key")
136+ clientCertPath = os .Getenv ("PGO_CLIENT_CERT" )
137+
138+ if clientCertPath == "" {
139+ log .Error ("PGO_CLIENT_CERT not specified" )
140+ os .Exit (2 )
141+ }
142+
143+ _ , err = ioutil .ReadFile (clientCertPath )
144+ if err != nil {
145+ log .Debug (clientCertPath + " not found" )
146+ os .Exit (2 )
147+ }
148+
149+ clientKeyPath = os .Getenv ("PGO_CLIENT_KEY" )
150+
151+ if clientKeyPath == "" {
152+ log .Error ("PGO_CLIENT_KEY not specified" )
153+ os .Exit (2 )
154+ }
155+
156+ _ , err = ioutil .ReadFile (clientKeyPath )
157+ if err != nil {
158+ log .Debug (clientKeyPath + " not found" )
159+ os .Exit (2 )
160+ }
161+ //cert, err = tls.LoadX509KeyPair("/tmp/example.com.crt", "/tmp/example.com.key")
162+ cert , err = tls .LoadX509KeyPair (clientCertPath , clientKeyPath )
123163 if err != nil {
124164 log .Fatal (err )
125- log.Error("could not load client. crt and client .key")
165+ log .Error ("could not load example.com. crt and example.com .key" )
126166 os .Exit (2 )
127- } */
167+ }
168+
169+ log .Info ("setting up httpclient with TLS" )
170+ httpclient = & http.Client {
171+ Transport : & http.Transport {
172+ TLSClientConfig : & tls.Config {
173+ RootCAs : caCertPool ,
174+ InsecureSkipVerify : true ,
175+ Certificates : []tls.Certificate {cert },
176+ },
177+ },
178+ }
128179
129180}
0 commit comments