conjur_api.go 3.2 KB

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