-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyhub.go
148 lines (118 loc) · 4.5 KB
/
keyhub.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package keyhub
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/coreos/go-oidc"
"golang.org/x/net/context"
"golang.org/x/oauth2/clientcredentials"
"github.com/dghubble/sling"
)
type Client struct {
ID string
Version *VersionService
Accounts *AccountService
Groups *GroupService
Systems *SystemService
ClientApplications *ClientApplicationService
Vaults *VaultService
ServiceAccounts *ServiceAccountService
LaunchPadTile *LaunchPadTileService
VersionErrors []error
}
// khJsonBodyProvider encodes a JSON tagged struct value as a Body for requests.
// See https://golang.org/pkg/encoding/json/#MarshalIndent for details.
type khJsonBodyProvider struct {
payload interface{}
}
func (p khJsonBodyProvider) ContentType() string {
return ""
}
func (p khJsonBodyProvider) Body() (io.Reader, error) {
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(p.payload)
if err != nil {
return nil, err
}
return buf, nil
}
func NewClientDefault(issuer string, clientID string, clientSecret string) (*Client, error) {
http.DefaultClient.Transport = http.DefaultTransport
if http.DefaultClient.Timeout == 0 {
http.DefaultClient.Timeout = time.Duration(time.Second * 10)
}
return NewClient(http.DefaultClient, issuer, clientID, clientSecret)
}
func NewClient(httpClient *http.Client, issuer string, clientID string, clientSecret string) (*Client, error) {
var err error
var baseVersionedSupported bool
var latestVersionedSupported bool
base := sling.New().Client(httpClient).Base(issuer)
versionService := newVersionService(base.New().Set("Accept", "application/json").Set("Content-Type", "application/json"))
newClient := &Client{
ID: clientID,
Version: versionService,
VersionErrors: make([]error, 0),
}
// Create sling for contract version 60
baseVersionedSling := base.New()
baseVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(60, baseVersionedSling)
if err != nil {
newClient.VersionErrors = append(newClient.VersionErrors, err)
}
// Create sling for contract version 71
latestVersionedSling := base.New()
latestVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(0, latestVersionedSling)
if err != nil {
newClient.VersionErrors = append(newClient.VersionErrors, err)
}
if !(baseVersionedSupported || latestVersionedSupported) {
return nil, fmt.Errorf("KeyHub %v does not support api contract versions 60 or Latest", versionService.info.KeyhubVersion)
}
ctx := oidc.ClientContext(context.Background(), httpClient)
provider, err := oidc.NewProvider(ctx, issuer)
if err != nil {
return nil, err
}
var appClientConf = clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Scopes: []string{oidc.ScopeOpenID},
TokenURL: provider.Endpoint().TokenURL + "?authVault=access",
}
oauth2Client := appClientConf.Client(ctx)
oauth2Client.Timeout = httpClient.Timeout
oauth2Sling := baseVersionedSling.New().Client(oauth2Client)
vaultClient := &http.Client{
Transport: &Transport{
Base: oauth2Client.Transport,
},
}
if baseVersionedSupported {
newClient.Accounts = newAccountService(oauth2Sling.New())
newClient.ClientApplications = newClientApplicationService(oauth2Sling.New())
newClient.Systems = newSystemService(oauth2Sling.New())
newClient.LaunchPadTile = newLaunchPadTileService(oauth2Sling.New())
newClient.ServiceAccounts = NewServiceAccountService(oauth2Sling)
}
if latestVersionedSupported {
newClient.Groups = newGroupService(latestVersionedSling.New().Client(oauth2Client))
newClient.Vaults = newVaultService(latestVersionedSling.New().Client(vaultClient))
}
return newClient, nil
}