conjur_api.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package conjur
  13. import (
  14. "fmt"
  15. "net/http"
  16. "strings"
  17. "time"
  18. "github.com/cyberark/conjur-api-go/conjurapi"
  19. "github.com/cyberark/conjur-api-go/conjurapi/authn"
  20. "github.com/cyberark/conjur-api-go/conjurapi/response"
  21. )
  22. // SecretsClient is an interface for the Conjur client.
  23. type SecretsClient interface {
  24. RetrieveSecret(secret string) (result []byte, err error)
  25. }
  26. // SecretsClientFactory is an interface for creating a Conjur client.
  27. type SecretsClientFactory interface {
  28. NewClientFromKey(config conjurapi.Config, loginPair authn.LoginPair) (SecretsClient, error)
  29. NewClientFromJWT(config conjurapi.Config, jwtToken string, jwtServiceID string) (SecretsClient, error)
  30. }
  31. // ClientAPIImpl is an implementation of the ClientAPI interface.
  32. type ClientAPIImpl struct{}
  33. func (c *ClientAPIImpl) NewClientFromKey(config conjurapi.Config, loginPair authn.LoginPair) (SecretsClient, error) {
  34. return conjurapi.NewClientFromKey(config, loginPair)
  35. }
  36. // NewClientFromJWT creates a new Conjur client from a JWT token.
  37. // cannot use the built-in function "conjurapi.NewClientFromJwt" because it requires environment variables
  38. // see: https://github.com/cyberark/conjur-api-go/blob/b698692392a38e5d38b8440f32ab74206544848a/conjurapi/client.go#L130
  39. func (c *ClientAPIImpl) NewClientFromJWT(config conjurapi.Config, jwtToken, jwtServiceID string) (SecretsClient, error) {
  40. jwtTokenString := fmt.Sprintf("jwt=%s", jwtToken)
  41. var httpClient *http.Client
  42. if config.IsHttps() {
  43. cert, err := config.ReadSSLCert()
  44. if err != nil {
  45. return nil, err
  46. }
  47. httpClient, err = newHTTPSClient(cert)
  48. if err != nil {
  49. return nil, err
  50. }
  51. } else {
  52. httpClient = &http.Client{Timeout: time.Second * 10}
  53. }
  54. authnJwtURL := strings.Join([]string{config.ApplianceURL, "authn-jwt", jwtServiceID, config.Account, "authenticate"}, "/")
  55. req, err := http.NewRequest("POST", authnJwtURL, strings.NewReader(jwtTokenString))
  56. if err != nil {
  57. return nil, err
  58. }
  59. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  60. resp, err := httpClient.Do(req)
  61. if err != nil {
  62. return nil, err
  63. }
  64. defer resp.Body.Close()
  65. tokenBytes, err := response.DataResponse(resp)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return conjurapi.NewClientFromToken(config, string(tokenBytes))
  70. }