forked from sethvargo/vault-init
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
124 lines (99 loc) · 2.67 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
package main
import (
"bytes"
"encoding/json"
"fmt"
//"io/ioutil"
"net/http"
"log"
)
var (
//httpClient *http.Client
)
type GithubAuthConfig struct {
Options []string `json:"options"`
DefaultLeaseTtl string `json:"default_lease_ttl"`
MaxLeaseTtl string `json:"max_lease_ttl"`
ForceNoCache bool `json:"force_no_cache"`
}
type GithubAuthRequest struct {
Type string `json:"type"`
Local bool `json:"local"`
SealWrap bool `json:"seal_wrap"`
ExternalEntropyAccess bool `json:"externl_entropy_access"`
Options []string `json:"options"`
Config GithubAuthConfig `json:"config"`
}
type GithubMapTeamRequest struct {
Team string `json:"team_name"`
Policies []string `json:"value"`
}
type PolicyCreateRequest struct {
Policy string `json:"policy"`
}
func mapGithubTeam(key string) (bool, error) {
return false, nil
}
//
// {"type":"github","description":"","config":{"options":null,"default_lease_ttl":"0s","max_lease_ttl":"0s","force_no_cache":false},"local":false,"seal_wrap":false,"external_entropy_access":false,"options":null}
//
func enableGithubAuth(token string) (bool, error) {
log.Println("Remove this way before releasing...")
log.Println(token)
authRequest := GithubAuthRequest{
}
authRequestData, err := json.Marshal(&authRequest)
if err != nil {
return false, err
}
r := bytes.NewReader(authRequestData)
request, err := http.NewRequest(http.MethodPut, vaultAddr+"/auth/github/config", r)
if err != nil {
return false, err
}
response, err := httpClient.Do(request)
if err != nil {
return false, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return false, fmt.Errorf("%d", response.StatusCode)
}
// Possibly not needed if a 200
//authRequestResponseBody, err := ioutil.ReadAll(response.Body)
//if err != nil {
// return false, err
//}
return true, nil
}
func createPolicy(token string, key string, policy string) (bool, error) {
policyRequest := PolicyCreateRequest{
Policy: policy,
}
policyRequestData, err := json.Marshal(&policyRequest)
if err != nil {
return false, err
}
r := bytes.NewReader(policyRequestData)
request, err := http.NewRequest(http.MethodPut, vaultAddr+"/v1/sys/policy/"+key, r)
response, err := httpClient.Do(request)
if err != nil {
return false, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return false, fmt.Errorf("policy creation: non-200 status code: %d", response.StatusCode)
}
return true, nil
}
func InitializeAuth(token string) (bool, error) {
//res, err := createPolicy(token, "vault_admins", "/policies/admin.hcl")
//if err != nil {
// return false, err
//}
_, err := enableGithubAuth(token)
if err != nil {
return false, err
}
return false, nil
}