@@ -30,13 +30,15 @@ import (
30
30
"strings"
31
31
"time"
32
32
33
+ "github.com/netflix/weep/pkg/httpAuth"
34
+ "github.com/netflix/weep/pkg/httpAuth/custom"
35
+
33
36
"github.com/netflix/weep/pkg/util"
34
37
35
38
"github.com/netflix/weep/pkg/aws"
36
39
"github.com/netflix/weep/pkg/config"
37
40
werrors "github.com/netflix/weep/pkg/errors"
38
41
"github.com/netflix/weep/pkg/httpAuth/challenge"
39
- "github.com/netflix/weep/pkg/httpAuth/mtls"
40
42
"github.com/netflix/weep/pkg/logging"
41
43
"github.com/netflix/weep/pkg/metadata"
42
44
@@ -48,8 +50,6 @@ import (
48
50
var clientVersion = fmt .Sprintf ("%s" , metadata .Version )
49
51
50
52
var userAgent = "weep/" + clientVersion + " Go-http-client/1.1"
51
- var clientFactoryOverride ClientFactory
52
- var preflightFunctions = make ([]RequestPreflight , 0 )
53
53
54
54
// HTTPClient is the interface we expect HTTP clients to implement.
55
55
type HTTPClient interface {
@@ -66,65 +66,15 @@ type Client struct {
66
66
Region string
67
67
}
68
68
69
- type ClientFactory func () (* http.Client , error )
70
-
71
- // RegisterClientFactory overrides Weep's standard config-based ConsoleMe client
72
- // creation with a ClientFactory. This function will be called during the creation
73
- // of all ConsoleMe clients.
74
- func RegisterClientFactory (factory ClientFactory ) {
75
- clientFactoryOverride = factory
76
- }
77
-
78
- type RequestPreflight func (req * http.Request ) error
79
-
80
- // RegisterRequestPreflight adds a RequestPreflight function which will be called in the
81
- // order of registration during the creation of a ConsoleMe request.
82
- func RegisterRequestPreflight (preflight RequestPreflight ) {
83
- preflightFunctions = append (preflightFunctions , preflight )
84
- }
85
-
86
69
// GetClient creates an authenticated ConsoleMe client
87
- func GetClient (region string ) (* Client , error ) {
70
+ func GetClient () (* Client , error ) {
88
71
var client * Client
89
72
consoleMeUrl := viper .GetString ("consoleme_url" )
90
- authenticationMethod := viper .GetString ("authentication_method" )
91
-
92
- if clientFactoryOverride != nil {
93
- customClient , err := clientFactoryOverride ()
94
- if err != nil {
95
- return client , err
96
- }
97
- client , err = NewClient (consoleMeUrl , "" , customClient )
98
- if err != nil {
99
- return client , err
100
- }
101
- } else if authenticationMethod == "mtls" {
102
- mtlsClient , err := mtls .NewHTTPClient ()
103
- if err != nil {
104
- return client , err
105
- }
106
- client , err = NewClient (consoleMeUrl , "" , mtlsClient )
107
- if err != nil {
108
- return client , err
109
- }
110
- } else if authenticationMethod == "challenge" {
111
- err := challenge .RefreshChallenge ()
112
- if err != nil {
113
- return client , err
114
- }
115
- httpClient , err := challenge .NewHTTPClient (consoleMeUrl )
116
- if err != nil {
117
- return client , err
118
- }
119
- client , err = NewClient (consoleMeUrl , "" , httpClient )
120
- if err != nil {
121
- return client , err
122
- }
123
- } else {
124
- return nil , fmt .Errorf ("Authentication method unsupported or not provided." )
73
+ httpClient , err := httpAuth .GetAuthenticatedClient ()
74
+ if err != nil {
75
+ return client , err
125
76
}
126
-
127
- return client , nil
77
+ return NewClient (consoleMeUrl , "" , httpClient )
128
78
}
129
79
130
80
// NewClient takes a ConsoleMe hostname and *http.Client, and returns a
@@ -147,18 +97,6 @@ func NewClient(hostname string, region string, httpc *http.Client) (*Client, err
147
97
return c , nil
148
98
}
149
99
150
- func runPreflightFunctions (req * http.Request ) error {
151
- var err error
152
- if preflightFunctions != nil {
153
- for _ , preflight := range preflightFunctions {
154
- if err = preflight (req ); err != nil {
155
- return err
156
- }
157
- }
158
- }
159
- return nil
160
- }
161
-
162
100
func (c * Client ) buildRequest (method string , resource string , body io.Reader , apiPrefix string ) (* http.Request , error ) {
163
101
urlStr := c .Host + apiPrefix + resource
164
102
req , err := http .NewRequest (method , urlStr , body )
@@ -167,7 +105,7 @@ func (c *Client) buildRequest(method string, resource string, body io.Reader, ap
167
105
}
168
106
req .Header .Set ("User-Agent" , userAgent )
169
107
req .Header .Add ("Content-Type" , "application/json" )
170
- err = runPreflightFunctions (req )
108
+ err = custom . RunPreflightFunctions (req )
171
109
if err != nil {
172
110
return nil , err
173
111
}
@@ -579,7 +517,7 @@ func GetCredentialsC(client HTTPClient, role string, ipRestrict bool, assumeRole
579
517
// GetCredentials requests credentials from ConsoleMe then follows the provided chain of roles to
580
518
// assume. Roles are assumed in the order in which they appear in the assumeRole slice.
581
519
func GetCredentials (role string , ipRestrict bool , assumeRole []string , region string ) (* aws.Credentials , error ) {
582
- client , err := GetClient (region )
520
+ client , err := GetClient ()
583
521
if err != nil {
584
522
return nil , err
585
523
}
0 commit comments