conjur_api.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "net/url"
  17. "strings"
  18. "time"
  19. "github.com/cyberark/conjur-api-go/conjurapi"
  20. "github.com/cyberark/conjur-api-go/conjurapi/authn"
  21. "github.com/cyberark/conjur-api-go/conjurapi/response"
  22. )
  23. // SecretsClient is an interface for the Conjur client.
  24. type SecretsClient interface {
  25. RetrieveSecret(secret string) (result []byte, err error)
  26. }
  27. // SecretsClientFactory is an interface for creating a Conjur client.
  28. type SecretsClientFactory interface {
  29. NewClientFromKey(config conjurapi.Config, loginPair authn.LoginPair) (SecretsClient, error)
  30. NewClientFromJWT(config conjurapi.Config, jwtToken string, jwtServiceID, jwtHostID string) (SecretsClient, error)
  31. }
  32. // ClientAPIImpl is an implementation of the ClientAPI interface.
  33. type ClientAPIImpl struct{}
  34. func (c *ClientAPIImpl) NewClientFromKey(config conjurapi.Config, loginPair authn.LoginPair) (SecretsClient, error) {
  35. return conjurapi.NewClientFromKey(config, loginPair)
  36. }
  37. // NewClientFromJWT creates a new Conjur client from a JWT token.
  38. // cannot use the built-in function "conjurapi.NewClientFromJwt" because it requires environment variables
  39. // see: https://github.com/cyberark/conjur-api-go/blob/b698692392a38e5d38b8440f32ab74206544848a/conjurapi/client.go#L130
  40. func (c *ClientAPIImpl) NewClientFromJWT(config conjurapi.Config, jwtToken, jwtServiceID, jwtHostID string) (SecretsClient, error) {
  41. jwtTokenString := fmt.Sprintf("jwt=%s", jwtToken)
  42. var httpClient *http.Client
  43. if config.IsHttps() {
  44. cert, err := config.ReadSSLCert()
  45. if err != nil {
  46. return nil, err
  47. }
  48. httpClient, err = newHTTPSClient(cert)
  49. if err != nil {
  50. return nil, err
  51. }
  52. } else {
  53. httpClient = &http.Client{Timeout: time.Second * 10}
  54. }
  55. var authnJwtURL string
  56. // If a hostID is provided, it must be included in the URL
  57. if jwtHostID != "" {
  58. authnJwtURL = strings.Join([]string{config.ApplianceURL, "authn-jwt", jwtServiceID, config.Account, url.PathEscape(jwtHostID), "authenticate"}, "/")
  59. } else {
  60. authnJwtURL = strings.Join([]string{config.ApplianceURL, "authn-jwt", jwtServiceID, config.Account, "authenticate"}, "/")
  61. }
  62. req, err := http.NewRequest("POST", authnJwtURL, strings.NewReader(jwtTokenString))
  63. if err != nil {
  64. return nil, err
  65. }
  66. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  67. resp, err := httpClient.Do(req)
  68. if err != nil {
  69. return nil, err
  70. }
  71. defer resp.Body.Close()
  72. tokenBytes, err := response.DataResponse(resp)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return conjurapi.NewClientFromToken(config, string(tokenBytes))
  77. }