-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.go
175 lines (141 loc) · 4.98 KB
/
iam.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
package iam
import (
"net/http"
"runtime/debug"
"time"
"github.com/selectel/iam-go/iamerrors"
baseclient "github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/federations/saml"
"github.com/selectel/iam-go/service/groups"
"github.com/selectel/iam-go/service/s3credentials"
"github.com/selectel/iam-go/service/serviceusers"
"github.com/selectel/iam-go/service/users"
)
const (
// appName represents an application name.
appName = "iam-go"
// defaultIAMApiURL represents a default Selectel IAM API URL.
defaultIAMApiURL = "https://api.selectel.ru"
// defaultHTTPTimeout represents the default timeout (in seconds) for HTTP requests.
defaultHTTPTimeout = 120
// defaultMaxIdleConns represents the maximum number of idle (keep-alive) connections.
defaultMaxIdleConns = 100
// defaultIdleConnTimeout represents the maximum amount of time an idle (keep-alive) connection will remain
// idle before closing itself.
defaultIdleConnTimeout = 100
// defaultTLSHandshakeTimeout represents the default timeout (in seconds) for TLS handshake.
defaultTLSHandshakeTimeout = 60
// defaultExpectContinueTimeout represents the default amount of time to wait for a server's first
// response headers.
defaultExpectContinueTimeout = 1
)
// Client stores the configuration, which is needed to make requests to the IAM API.
type Client struct {
// authOpts contains data to authenticate against Selectel IAM API.
authOpts *AuthOpts
// baseClient contains the configuration of the Client.
baseClient *baseclient.BaseClient
// Users instance is used to make requests against Selectel IAM API and manage Panel Users.
Users *users.Service
// ServiceUsers instance is used to make requests against Selectel IAM API and manage Service Users.
ServiceUsers *serviceusers.Service
// Groups instance is used to make requests against Selectel IAM API and manage Groups of users.
Groups *groups.Service
// S3Credentials instance is used to make requests against Selectel IAM API and manage S3 Credentials.
S3Credentials *s3credentials.Service
// SAMLFederations instance is used to make requests against Selectel IAM API and manage SAML Federations.
// It also contains Certificates service, which is used to manage certificates.
SAMLFederations *saml.Service
}
type AuthOpts struct {
KeystoneToken string
}
type Option func(*Client)
// WithAPIUrl is a functional parameter for Client, used to set IAM API URL.
func WithAPIUrl(url string) Option {
return func(c *Client) {
c.baseClient.APIUrl = url
}
}
// WithCustomHTTPClient is a functional parameter for Client, used to set a custom HTTP client.
func WithCustomHTTPClient(httpClient *http.Client) Option {
return func(c *Client) {
c.baseClient.HTTPClient = httpClient
}
}
// WithAuthOpts is a functional parameter for Client, used to set on of implementations of AuthType.
func WithAuthOpts(authOpts *AuthOpts) Option {
return func(c *Client) {
c.authOpts = authOpts
}
}
// WithUserAgentPrefix is a functional parameter for Client, used to set a custom prefix.
func WithUserAgentPrefix(prefix string) Option {
return func(c *Client) {
c.baseClient.UserAgentPrefix = prefix
}
}
// New returns a new instance of Client for the v1 IAM API.
func New(opts ...Option) (*Client, error) {
c := &Client{baseClient: &baseclient.BaseClient{}}
for _, opt := range opts {
opt(c)
}
if !c.validateAndSetAuthMethod() {
return nil, iamerrors.Error{Err: iamerrors.ErrClientNoAuthOpts, Desc: "No AuthOpts was passed"}
}
if c.baseClient.APIUrl == "" {
c.baseClient.APIUrl = defaultIAMApiURL
}
if c.baseClient.HTTPClient == nil {
c.baseClient.HTTPClient = &http.Client{
Timeout: defaultHTTPTimeout * time.Second,
Transport: newHTTPTransport(),
}
}
appVersion := findModuleVersion()
userAgent := appName + "/" + appVersion
if c.baseClient.UserAgentPrefix == "" {
c.baseClient.UserAgent = userAgent
} else {
c.baseClient.UserAgent = c.baseClient.UserAgentPrefix + " " + userAgent
}
c.Users = users.New(c.baseClient)
c.ServiceUsers = serviceusers.New(c.baseClient)
c.Groups = groups.New(c.baseClient)
c.S3Credentials = s3credentials.New(c.baseClient)
c.SAMLFederations = saml.New(c.baseClient)
return c, nil
}
func (c *Client) validateAndSetAuthMethod() bool {
if c.authOpts == nil {
return false
}
if c.authOpts.KeystoneToken != "" {
c.baseClient.AuthMethod = &baseclient.KeystoneTokenAuth{
KeystoneToken: c.authOpts.KeystoneToken,
}
return true
}
return false
}
func newHTTPTransport() *http.Transport {
return &http.Transport{
MaxIdleConns: defaultMaxIdleConns,
IdleConnTimeout: defaultIdleConnTimeout * time.Second,
TLSHandshakeTimeout: defaultTLSHandshakeTimeout * time.Second,
ExpectContinueTimeout: defaultExpectContinueTimeout * time.Second,
}
}
func findModuleVersion() string {
moduleName := "github.com/selectel/" + appName
info, ok := debug.ReadBuildInfo()
if ok {
for _, dep := range info.Deps {
if dep.Path == moduleName {
return dep.Version
}
}
}
return "v0.1.0"
}