@@ -21,7 +21,10 @@ Please review third_party pinning scripts and patches for more details.
2121package lib
2222
2323import (
24+ "encoding/json"
25+ "fmt"
2426 "net/http"
27+ "strconv"
2528
2629 "github.com/pkg/errors"
2730
@@ -141,6 +144,166 @@ func (i *Identity) Revoke(req *api.RevocationRequest) (*api.RevocationResponse,
141144 return & api.RevocationResponse {RevokedCerts : result .RevokedCerts , CRL : crl }, nil
142145}
143146
147+ // GetIdentity returns information about the requested identity
148+ func (i * Identity ) GetIdentity (id , caname string ) (* api.GetIDResponse , error ) {
149+ log .Debugf ("Entering identity.GetIdentity %s" , id )
150+ result := & api.GetIDResponse {}
151+ err := i .Get (fmt .Sprintf ("identities/%s" , id ), caname , result )
152+ if err != nil {
153+ return nil , err
154+ }
155+
156+ log .Debugf ("Successfully retrieved identity: %+v" , result )
157+ return result , nil
158+ }
159+
160+ // GetAllIdentities returns all identities that the caller is authorized to see
161+ func (i * Identity ) GetAllIdentities (caname string , cb func (* json.Decoder ) error ) error {
162+ log .Debugf ("Entering identity.GetAllIdentities" )
163+ err := i .GetStreamResponse ("identities" , caname , "result.identities" , cb )
164+ if err != nil {
165+ return err
166+ }
167+ log .Debugf ("Successfully retrieved identities" )
168+ return nil
169+ }
170+
171+ // AddIdentity adds a new identity to the server
172+ func (i * Identity ) AddIdentity (req * api.AddIdentityRequest ) (* api.IdentityResponse , error ) {
173+ log .Debugf ("Entering identity.AddIdentity with request: %+v" , req )
174+ if req .ID == "" {
175+ return nil , errors .New ("Adding identity with no 'ID' set" )
176+ }
177+
178+ reqBody , err := util .Marshal (req , "addIdentity" )
179+ if err != nil {
180+ return nil , err
181+ }
182+
183+ // Send a post to the "identities" endpoint with req as body
184+ result := & api.IdentityResponse {}
185+ err = i .Post ("identities" , reqBody , result , nil )
186+ if err != nil {
187+ return nil , err
188+ }
189+
190+ log .Debugf ("Successfully added new identity '%s'" , result .ID )
191+ return result , nil
192+ }
193+
194+ // ModifyIdentity modifies an existing identity on the server
195+ func (i * Identity ) ModifyIdentity (req * api.ModifyIdentityRequest ) (* api.IdentityResponse , error ) {
196+ log .Debugf ("Entering identity.ModifyIdentity with request: %+v" , req )
197+ if req .ID == "" {
198+ return nil , errors .New ("Name of identity to be modified not specified" )
199+ }
200+
201+ reqBody , err := util .Marshal (req , "modifyIdentity" )
202+ if err != nil {
203+ return nil , err
204+ }
205+
206+ // Send a put to the "identities" endpoint with req as body
207+ result := & api.IdentityResponse {}
208+ err = i .Put (fmt .Sprintf ("identities/%s" , req .ID ), reqBody , nil , result )
209+ if err != nil {
210+ return nil , err
211+ }
212+
213+ log .Debugf ("Successfully modified identity '%s'" , result .ID )
214+ return result , nil
215+ }
216+
217+ // RemoveIdentity removes a new identity from the server
218+ func (i * Identity ) RemoveIdentity (req * api.RemoveIdentityRequest ) (* api.IdentityResponse , error ) {
219+ log .Debugf ("Entering identity.RemoveIdentity with request: %+v" , req )
220+ id := req .ID
221+ if id == "" {
222+ return nil , errors .New ("Name of the identity to removed is required" )
223+ }
224+
225+ // Send a delete to the "identities" endpoint id as a path parameter
226+ result := & api.IdentityResponse {}
227+ queryParam := make (map [string ]string )
228+ queryParam ["force" ] = strconv .FormatBool (req .Force )
229+ queryParam ["ca" ] = req .CAName
230+ err := i .Delete (fmt .Sprintf ("identities/%s" , id ), result , queryParam )
231+ if err != nil {
232+ return nil , err
233+ }
234+
235+ log .Debugf ("Successfully removed identity: %s" , id )
236+ return result , nil
237+ }
238+
239+ // Get sends a get request to an endpoint
240+ func (i * Identity ) Get (endpoint , caname string , result interface {}) error {
241+ req , err := i .client .newGet (endpoint )
242+ if err != nil {
243+ return err
244+ }
245+ if caname != "" {
246+ addQueryParm (req , "ca" , caname )
247+ }
248+ err = i .addTokenAuthHdr (req , nil )
249+ if err != nil {
250+ return err
251+ }
252+ return i .client .SendReq (req , result )
253+ }
254+
255+ // GetStreamResponse sends a request to an endpoint and streams the response
256+ func (i * Identity ) GetStreamResponse (endpoint , caname , stream string , cb func (* json.Decoder ) error ) error {
257+ req , err := i .client .newGet (endpoint )
258+ if err != nil {
259+ return err
260+ }
261+ if caname != "" {
262+ addQueryParm (req , "ca" , caname )
263+ }
264+ err = i .addTokenAuthHdr (req , nil )
265+ if err != nil {
266+ return err
267+ }
268+ return i .client .StreamResponse (req , stream , cb )
269+ }
270+
271+ // Put sends a put request to an endpoint
272+ func (i * Identity ) Put (endpoint string , reqBody []byte , queryParam map [string ]string , result interface {}) error {
273+ req , err := i .client .newPut (endpoint , reqBody )
274+ if err != nil {
275+ return err
276+ }
277+ if queryParam != nil {
278+ for key , value := range queryParam {
279+ addQueryParm (req , key , value )
280+ }
281+ }
282+ err = i .addTokenAuthHdr (req , reqBody )
283+ if err != nil {
284+ return err
285+ }
286+ return i .client .SendReq (req , result )
287+ }
288+
289+ // Delete sends a delete request to an endpoint
290+ func (i * Identity ) Delete (endpoint string , result interface {}, queryParam map [string ]string ) error {
291+ req , err := i .client .newDelete (endpoint )
292+ if err != nil {
293+ return err
294+ }
295+ if queryParam != nil {
296+ for key , value := range queryParam {
297+ addQueryParm (req , key , value )
298+ }
299+ }
300+ err = i .addTokenAuthHdr (req , nil )
301+ if err != nil {
302+ return err
303+ }
304+ return i .client .SendReq (req , result )
305+ }
306+
144307// Post sends arbitrary request body (reqBody) to an endpoint.
145308// This adds an authorization header which contains the signature
146309// of this identity over the body and non-signature part of the authorization header.
0 commit comments