|
| 1 | +package cmd |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2017 Crunchy Data Solutions, Inc. |
| 5 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + you may not use this file except in compliance with the License. |
| 7 | + You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | + Unless required by applicable law or agreed to in writing, software |
| 12 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + See the License for the specific language governing permissions and |
| 15 | + limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import ( |
| 19 | + log "github.com/Sirupsen/logrus" |
| 20 | + "io/ioutil" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + "runtime" |
| 24 | + "strings" |
| 25 | +) |
| 26 | + |
| 27 | +const etcpath = "/etc/pgo/pgouser" |
| 28 | +const pgouserenvvar = "PGOUSER" |
| 29 | + |
| 30 | +// BasicAuthUsername and BasicAuthPassword are for BasicAuth, they are fetched from a file |
| 31 | +var BasicAuthUsername, BasicAuthPassword string |
| 32 | + |
| 33 | +// StatusCheck ... |
| 34 | +func StatusCheck(resp *http.Response) { |
| 35 | + log.Debugf("http status code is %d\n", resp.StatusCode) |
| 36 | + if resp.StatusCode == 401 { |
| 37 | + log.Fatalf("Authentication Failed: %d\n", resp.StatusCode) |
| 38 | + os.Exit(2) |
| 39 | + } else if resp.StatusCode != 200 { |
| 40 | + log.Fatalf("Invalid Status Code: %d\n", resp.StatusCode) |
| 41 | + os.Exit(2) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func UserHomeDir() string { |
| 46 | + env := "HOME" |
| 47 | + if runtime.GOOS == "windows" { |
| 48 | + env = "USERPROFILE" |
| 49 | + } else if runtime.GOOS == "plan9" { |
| 50 | + env = "home" |
| 51 | + } |
| 52 | + return os.Getenv(env) |
| 53 | +} |
| 54 | + |
| 55 | +func parseCredentials(dat string) (string, string) { |
| 56 | + |
| 57 | + fields := strings.Split(strings.TrimSpace(dat), ":") |
| 58 | + log.Debugf("%v\n", fields) |
| 59 | + log.Debugf("username=[%s] password=[%s]\n", fields[0], fields[1]) |
| 60 | + return fields[0], fields[1] |
| 61 | +} |
| 62 | + |
| 63 | +func GetCredentials() { |
| 64 | + log.Debug("GetCredentials called") |
| 65 | + |
| 66 | + dir := UserHomeDir() |
| 67 | + fullPath := dir + "/" + ".pgouser" |
| 68 | + log.Debug("looking in " + fullPath + " for credentials") |
| 69 | + dat, err := ioutil.ReadFile(fullPath) |
| 70 | + if err != nil { |
| 71 | + log.Debug(fullPath + " not found") |
| 72 | + } else { |
| 73 | + log.Debug(fullPath + " found") |
| 74 | + log.Debug("pgouser file found at " + fullPath + "contains " + string(dat)) |
| 75 | + BasicAuthUsername, BasicAuthPassword = parseCredentials(string(dat)) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + fullPath = etcpath |
| 80 | + dat, err = ioutil.ReadFile(fullPath) |
| 81 | + if err != nil { |
| 82 | + log.Debug(etcpath + " not found") |
| 83 | + } else { |
| 84 | + log.Debug(fullPath + " found") |
| 85 | + log.Debug("pgouser file found at " + fullPath + "contains " + string(dat)) |
| 86 | + BasicAuthUsername, BasicAuthPassword = parseCredentials(string(dat)) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + pgoUser := os.Getenv(pgouserenvvar) |
| 91 | + if pgoUser == "" { |
| 92 | + log.Error(pgouserenvvar + " env var not set") |
| 93 | + os.Exit(2) |
| 94 | + } |
| 95 | + |
| 96 | + fullPath = pgoUser |
| 97 | + log.Debug(pgouserenvvar + " env var is being used at " + fullPath) |
| 98 | + dat, err = ioutil.ReadFile(fullPath) |
| 99 | + if err != nil { |
| 100 | + log.Error(fullPath + " file not found") |
| 101 | + os.Exit(2) |
| 102 | + } |
| 103 | + |
| 104 | + log.Debug("pgouser file found at " + fullPath + "contains " + string(dat)) |
| 105 | + BasicAuthUsername, BasicAuthPassword = parseCredentials(string(dat)) |
| 106 | +} |
0 commit comments