forked from brian1917/vcodeHMAC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
credentials.go
63 lines (52 loc) · 1.61 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
package vcodeHMAC
import (
"bufio"
"os"
"strings"
)
func getCredentials(fileString string, profile string) ([2]string, error) {
var credentials [2]string
profileLine := "[" + profile + "]"
foundProfile := false
foundKey := false
foundSecret := false
file, err := os.Open(fileString)
if err != nil {
return credentials, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if len(line) == 0 || line[0] == '#' {
// skip empty lines or lines starting with '#'
continue
}
if line == profileLine {
foundProfile = true
continue
}
// dont even bother with anything until youve found the profile
if foundProfile == false {
continue
}
//We remove spaces to account for discrepancies in user configuration of creds file
if strings.Contains(scanner.Text(), "veracode_api_key_id") {
removeSpaces := strings.Replace(scanner.Text(), " ", "", -1)
credentials[0] = strings.Replace(removeSpaces, "veracode_api_key_id=", "", -1)
foundKey = true
} else if strings.Contains(scanner.Text(), "veracode_api_key_secret") {
removeSpaces := strings.Replace(scanner.Text(), " ", "", -1)
credentials[1] = strings.Replace(removeSpaces, "veracode_api_key_secret=", "", -1)
foundSecret = true
}
if foundKey == true && foundSecret == true {
break
}
}
if err := scanner.Err(); err != nil {
return credentials, err
}
return credentials, nil
}