/
v2_config.go
226 lines (196 loc) · 6.01 KB
/
v2_config.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// DO NOT EDIT LOCAL SDK - USE v3 okta-sdk-golang FOR API CALLS THAT DO NOT EXIST IN LOCAL SDK
package sdk
import (
"errors"
"log"
"net/http"
"os"
"syscall"
"github.com/okta/terraform-provider-okta/sdk/cache"
"gopkg.in/square/go-jose.v2"
)
type config struct {
Okta struct {
Client struct {
Cache struct {
Enabled bool `yaml:"enabled" envconfig:"OKTA_CLIENT_CACHE_ENABLED"`
DefaultTtl int32 `yaml:"defaultTtl" envconfig:"OKTA_CLIENT_CACHE_DEFAULT_TTL"`
DefaultTti int32 `yaml:"defaultTti" envconfig:"OKTA_CLIENT_CACHE_DEFAULT_TTI"`
} `yaml:"cache"`
Proxy struct {
Port int32 `yaml:"port" envconfig:"OKTA_CLIENT_PROXY_PORT"`
Host string `yaml:"host" envconfig:"OKTA_CLIENT_PROXY_HOST"`
Username string `yaml:"username" envconfig:"OKTA_CLIENT_PROXY_USERNAME"`
Password string `yaml:"password" envconfig:"OKTA_CLIENT_PROXY_PASSWORD"`
} `yaml:"proxy"`
ConnectionTimeout int64 `yaml:"connectionTimeout" envconfig:"OKTA_CLIENT_CONNECTION_TIMEOUT"`
RequestTimeout int64 `yaml:"requestTimeout" envconfig:"OKTA_CLIENT_REQUEST_TIMEOUT"`
RateLimit struct {
MaxRetries int32 `yaml:"maxRetries" envconfig:"OKTA_CLIENT_RATE_LIMIT_MAX_RETRIES"`
MaxBackoff int64 `yaml:"maxBackoff" envconfig:"OKTA_CLIENT_RATE_LIMIT_MAX_BACKOFF"`
} `yaml:"rateLimit"`
OrgUrl string `yaml:"orgUrl" envconfig:"OKTA_CLIENT_ORGURL"`
Token string `yaml:"token" envconfig:"OKTA_CLIENT_TOKEN"`
AuthorizationMode string `yaml:"authorizationMode" envconfig:"OKTA_CLIENT_AUTHORIZATIONMODE"`
ClientId string `yaml:"clientId" envconfig:"OKTA_CLIENT_CLIENTID"`
Scopes []string `yaml:"scopes" envconfig:"OKTA_CLIENT_SCOPES"`
PrivateKey string `yaml:"privateKey" envconfig:"OKTA_CLIENT_PRIVATEKEY"`
PrivateKeyId string `yaml:"privateKeyId" envconfig:"OKTA_CLIENT_PRIVATEKEYID"`
ClientAssertion string `yaml:"clientAssertion" envconfig:"OKTA_CLIENT_CLIENTASSERTION"`
} `yaml:"client"`
Testing struct {
DisableHttpsCheck bool `yaml:"disableHttpsCheck" envconfig:"OKTA_TESTING_DISABLE_HTTPS_CHECK"`
} `yaml:"testing"`
} `yaml:"okta"`
UserAgentExtra string
HttpClient *http.Client
CacheManager cache.Cache
PrivateKeySigner jose.Signer
}
type ConfigSetter func(*config)
func WithCache(cache bool) ConfigSetter {
return func(c *config) {
c.Okta.Client.Cache.Enabled = cache
}
}
func WithCacheManager(cacheManager cache.Cache) ConfigSetter {
return func(c *config) {
c.CacheManager = cacheManager
}
}
func WithCacheTtl(i int32) ConfigSetter {
return func(c *config) {
c.Okta.Client.Cache.DefaultTtl = i
}
}
func WithCacheTti(i int32) ConfigSetter {
return func(c *config) {
c.Okta.Client.Cache.DefaultTti = i
}
}
func WithConnectionTimeout(i int64) ConfigSetter {
return func(c *config) {
c.Okta.Client.ConnectionTimeout = i
}
}
func WithProxyPort(i int32) ConfigSetter {
return func(c *config) {
c.Okta.Client.Proxy.Port = i
}
}
func WithProxyHost(host string) ConfigSetter {
return func(c *config) {
c.Okta.Client.Proxy.Host = host
}
}
func WithProxyUsername(username string) ConfigSetter {
return func(c *config) {
c.Okta.Client.Proxy.Username = username
}
}
func WithProxyPassword(pass string) ConfigSetter {
return func(c *config) {
c.Okta.Client.Proxy.Password = pass
}
}
func WithOrgUrl(url string) ConfigSetter {
return func(c *config) {
c.Okta.Client.OrgUrl = url
}
}
func WithToken(token string) ConfigSetter {
return func(c *config) {
c.Okta.Client.Token = token
}
}
func WithUserAgentExtra(userAgent string) ConfigSetter {
return func(c *config) {
c.UserAgentExtra = userAgent
}
}
// Deprecated: please use WithHttpClientPtr method
func WithHttpClient(httpClient http.Client) ConfigSetter {
return func(c *config) {
c.HttpClient = &httpClient
}
}
func WithHttpClientPtr(httpClient *http.Client) ConfigSetter {
return func(c *config) {
c.HttpClient = httpClient
}
}
func WithTestingDisableHttpsCheck(httpsCheck bool) ConfigSetter {
return func(c *config) {
c.Okta.Testing.DisableHttpsCheck = httpsCheck
}
}
func WithRequestTimeout(requestTimeout int64) ConfigSetter {
return func(c *config) {
c.Okta.Client.RequestTimeout = requestTimeout
}
}
func WithRateLimitMaxRetries(maxRetries int32) ConfigSetter {
return func(c *config) {
c.Okta.Client.RateLimit.MaxRetries = maxRetries
}
}
func WithRateLimitMaxBackOff(maxBackoff int64) ConfigSetter {
return func(c *config) {
c.Okta.Client.RateLimit.MaxBackoff = maxBackoff
}
}
func WithAuthorizationMode(authzMode string) ConfigSetter {
return func(c *config) {
c.Okta.Client.AuthorizationMode = authzMode
}
}
func WithClientId(clientId string) ConfigSetter {
return func(c *config) {
c.Okta.Client.ClientId = clientId
}
}
func WithClientAssertion(clientAssertion string) ConfigSetter {
return func(c *config) {
c.Okta.Client.ClientAssertion = clientAssertion
}
}
func WithScopes(scopes []string) ConfigSetter {
return func(c *config) {
c.Okta.Client.Scopes = scopes
}
}
// WithPrivateKey sets private key key. Can be either a path to a private key or private key itself.
func WithPrivateKey(privateKey string) ConfigSetter {
return func(c *config) {
if fileExists(privateKey) {
content, err := os.ReadFile(privateKey)
if err != nil {
log.Fatalf("failed to read from provided private key file path: %v", err)
}
c.Okta.Client.PrivateKey = string(content)
} else {
c.Okta.Client.PrivateKey = privateKey
}
}
}
func WithPrivateKeyId(privateKeyId string) ConfigSetter {
return func(c *config) {
c.Okta.Client.PrivateKeyId = privateKeyId
}
}
func WithPrivateKeySigner(signer jose.Signer) ConfigSetter {
return func(c *config) {
c.PrivateKeySigner = signer
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) || errors.Is(err, syscall.ENAMETOOLONG) {
return false
}
log.Println("can not get information about the file containing private key, using provided value as the key itself")
return false
}
return !info.IsDir()
}