@@ -44,15 +44,19 @@ func NewClient(port int) *Client {
44
44
45
45
func (c * Client ) LoadUrl (profile string ) string {
46
46
if profile == "" {
47
- return fmt .Sprintf ("http://localhost:%d%s " , c .port , CREDS_ROUTE )
47
+ return fmt .Sprintf ("http://localhost:%d/ " , c .port )
48
48
}
49
- return fmt .Sprintf ("http://localhost:%d%s?profile= %s" , c .port , CREDS_ROUTE , url .QueryEscape (profile ))
49
+ return fmt .Sprintf ("http://localhost:%d%s/ %s" , c .port , SLOT_ROUTE , url .QueryEscape (profile ))
50
50
}
51
51
52
52
func (c * Client ) ProfileUrl () string {
53
53
return fmt .Sprintf ("http://localhost:%d%s" , c .port , PROFILE_ROUTE )
54
54
}
55
55
56
+ func (c * Client ) ListUrl () string {
57
+ return fmt .Sprintf ("http://localhost:%d%s" , c .port , SLOT_ROUTE )
58
+ }
59
+
56
60
type ClientRequest struct {
57
61
Creds * storage.RoleCredentials `json:"Creds"`
58
62
ProfileName string `json:"ProfileName"`
@@ -78,48 +82,41 @@ func (c *Client) SubmitCreds(creds *storage.RoleCredentials, profile string, slo
78
82
}
79
83
req .Header .Set ("Content-Type" , CHARSET_JSON )
80
84
client := & http.Client {}
81
- _ , err = client .Do (req )
82
- return err
83
- }
84
-
85
- func (c * Client ) GetProfile () (string , error ) {
86
- req , err := http .NewRequest (http .MethodGet , c .ProfileUrl (), bytes .NewBuffer ([]byte ("" )))
85
+ resp , err := client .Do (req )
87
86
if err != nil {
88
- return "" , err
87
+ return err
89
88
}
89
+ return CheckDoResponse (resp )
90
+ }
91
+
92
+ type ListProfilesResponse struct {
93
+ ProfileName string `json:"ProfileName" header:"ProfileName"`
94
+ AccountIdPad string `json:"AccountId" header:"AccountIdPad"`
95
+ RoleName string `json:"RoleName" header:"RoleName"`
96
+ Expiration int64 `json:"Expiration" header:"Expiration"`
97
+ Expires string `json:"Expires" header:"Expires"`
98
+ }
90
99
100
+ func (c * Client ) GetProfile () (ListProfilesResponse , error ) {
101
+ lpr := ListProfilesResponse {}
91
102
client := & http.Client {}
92
- req . Header . Set ( "Content-Type" , CHARSET_JSON )
103
+ resp , err := client . Get ( c . ProfileUrl () )
93
104
if err != nil {
94
- return "" , err
95
- }
96
- resp , err := client .Do (req )
97
- if err != nil {
98
- return "" , err
105
+ return lpr , err
99
106
}
100
107
defer resp .Body .Close ()
101
108
102
109
body , err := io .ReadAll (resp .Body )
103
110
if err != nil {
104
- return "" , err
111
+ return lpr , err
105
112
}
106
113
107
- m := map [string ]string {}
108
- err = json .Unmarshal (body , & m )
109
- if err != nil {
110
- return "" , err
114
+ if err = json .Unmarshal (body , & lpr ); err != nil {
115
+ return lpr , err
111
116
}
112
- log .Debugf ("resp: %s" , spew .Sdump (m ))
113
-
114
- return m ["profile" ], nil
115
- }
117
+ log .Debugf ("resp: %s" , spew .Sdump (lpr ))
116
118
117
- type ListProfilesResponse struct {
118
- ProfileName string `json:"ProfileName" header:"ProfileName"`
119
- AccountIdPad string `json:"AccountId" header:"AccountIdPad"`
120
- RoleName string `json:"RoleName" header:"RoleName"`
121
- Expiration int64 `json:"Expiration" header:"Expiration"`
122
- Expires string `json:"Expires" header:"Expires"`
119
+ return lpr , nil
123
120
}
124
121
125
122
// GetHeader is required for GenerateTable()
@@ -131,17 +128,8 @@ func (lpr ListProfilesResponse) GetHeader(fieldName string) (string, error) {
131
128
// ListProfiles returns a list of profiles that are loaded into slots
132
129
func (c * Client ) ListProfiles () ([]ListProfilesResponse , error ) {
133
130
lpr := []ListProfilesResponse {}
134
- req , err := http .NewRequest (http .MethodGet , c .LoadUrl ("" ), bytes .NewBuffer ([]byte ("" )))
135
- if err != nil {
136
- return lpr , err
137
- }
138
-
139
131
client := & http.Client {}
140
- req .Header .Set ("Content-Type" , CHARSET_JSON )
141
- if err != nil {
142
- return lpr , err
143
- }
144
- resp , err := client .Do (req )
132
+ resp , err := client .Get (c .ListUrl ())
145
133
if err != nil {
146
134
return lpr , err
147
135
}
@@ -152,8 +140,7 @@ func (c *Client) ListProfiles() ([]ListProfilesResponse, error) {
152
140
return lpr , err
153
141
}
154
142
155
- err = json .Unmarshal (body , & lpr )
156
- if err != nil {
143
+ if err = json .Unmarshal (body , & lpr ); err != nil {
157
144
return lpr , err
158
145
}
159
146
log .Debugf ("resp: %s" , spew .Sdump (lpr ))
@@ -172,6 +159,31 @@ func (c *Client) Delete(profile string) error {
172
159
if err != nil {
173
160
return err
174
161
}
175
- _ , err = client .Do (req )
176
- return err
162
+ resp , err := client .Do (req )
163
+ if err != nil {
164
+ return err
165
+ }
166
+ return CheckDoResponse (resp )
167
+ }
168
+
169
+ // ReadClientRequest unmarshals the client's request into our ClientRequest struct
170
+ // used to load new credentials into the server
171
+ func ReadClientRequest (r * http.Request ) (* ClientRequest , error ) {
172
+ defer r .Body .Close ()
173
+ body , err := io .ReadAll (r .Body )
174
+ if err != nil {
175
+ return & ClientRequest {}, fmt .Errorf ("reading body: %s" , err .Error ())
176
+ }
177
+ req := & ClientRequest {}
178
+ if err = json .Unmarshal (body , req ); err != nil {
179
+ return & ClientRequest {}, fmt .Errorf ("parsing json: %s" , err .Error ())
180
+ }
181
+ return req , nil
182
+ }
183
+
184
+ func CheckDoResponse (resp * http.Response ) error {
185
+ if resp .StatusCode < 200 || resp .StatusCode > 200 {
186
+ return fmt .Errorf ("HTTP Error %d" , resp .StatusCode )
187
+ }
188
+ return nil
177
189
}
0 commit comments