forked from chef/chef-load
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
54 lines (47 loc) · 1.21 KB
/
util.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
package main
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/go-chef/chef"
)
func getApiClient(clientName, privateKeyPath, chefServerUrl string) chef.Client {
privateKey := getPrivateKey(privateKeyPath)
client, err := chef.NewClient(&chef.Config{
Name: clientName,
Key: privateKey,
BaseURL: chefServerUrl,
SkipSSL: true,
})
if err != nil {
fmt.Println("Issue setting up client:", err)
}
return *client
}
func getStatusCode(err error) int {
errFields := strings.Fields(err.Error())
statusCode, _ := strconv.Atoi(errFields[len(errFields)-1])
return statusCode
}
func getPrivateKey(privateKeyPath string) string {
fileContent, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
fmt.Println("Couldn't read private key:", err)
}
privateKey := string(fileContent)
return privateKey
}
func getPublicKey(privateKeyPath string) string {
privateKey := getPrivateKey(privateKeyPath)
rsaPrivateKey, _ := chef.PrivateKeyFromString([]byte(privateKey))
rsaPublicKey := rsaPrivateKey.Public()
PubASN1, _ := x509.MarshalPKIXPublicKey(rsaPublicKey)
publicKey := string(pem.EncodeToMemory(&pem.Block{
Type: "PUBLIC KEY",
Bytes: PubASN1,
}))
return publicKey
}