Skip to content

Commit 9331206

Browse files
authored
Merge pull request #189 from cyberark/rest_client_options
Custom rest_client_options via Conjur configuration
2 parents c5a1dd7 + a07c933 commit 9331206

File tree

12 files changed

+325
-153
lines changed

12 files changed

+325
-153
lines changed

.gitleaks.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
title = "Secretless Broker gitleaks config"
1+
title = "Conjur API Ruby gitleaks config"
22

33
# This is the config file for gitleaks. You can configure gitleaks what to search for and what to whitelist.
44
# If GITLEAKS_CONFIG environment variable

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
- Add `rest_client_options` option to `Conjur.configuration`. This allows users to
11+
configure the RestClient instance used by Conjur API to communicate with the Conjur
12+
server.
13+
[cyberark/conjur-api-ruby#188](https://github.com/cyberark/conjur-api-ruby/issues/188)
14+
15+
### Changed
16+
- Replace monkey patching `RestClient::Request` with defaults on `Conjur.configuration.rest_client_options`
17+
in order to limit the scope of the default `:ssl_cert_store` option only to inside
18+
Conjur API.
19+
[cyberark/conjur-api-ruby#188](https://github.com/cyberark/conjur-api-ruby/issues/188)
20+
921
## [5.3.4] - 2020-10-29
1022

1123
### Changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ Conjur::API.new_from_key login, api_key
128128
Note that if you are connecting as a [Host](http://developer.conjur.net/reference/services/directory/host), the login should be
129129
prefixed with `host/`. For example: `host/myhost.example.com`, not just `myhost.example.com`.
130130

131+
## Configuring RestClient
132+
133+
[Conjur::Configuration](https://github.com/conjurinc/api-ruby/blob/master/lib/conjur/configuration.rb)
134+
allows optional configuration of the [RestClient](https://github.com/rest-client/rest-client)
135+
instance used by Conjur API to communicate with the Conjur server, via the options hash
136+
`Conjur.configuration.rest_client_options`.
137+
138+
The default value for the options hash is:
139+
```ruby
140+
{
141+
ssl_cert_store: OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
142+
}
143+
```
144+
145+
For example, here's how you would configure the client to use a proxy and `ssl_ca_file` (instead of the default `ssl_cert_store`).
146+
```ruby
147+
Conjur.configuration.rest_client_options = {
148+
ssl_ca_file: "ca_certificate.pem",
149+
proxy: "http://proxy.example.com/"
150+
}
151+
```
152+
131153
## Contributing
132154

133155
We welcome contributions of all kinds to this repository. For instructions on how to get started and descriptions of our development workflows, please see our [contributing

lib/conjur/api.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,6 @@
5050
require 'conjur/cache'
5151
require 'conjur-api/version'
5252

53-
# Monkey patch RestClient::Request so it always uses
54-
# :ssl_cert_store. (RestClient::Resource uses Request to send
55-
# requests, so it sees :ssl_cert_store, too).
56-
# @api private
57-
class RestClient::Request
58-
alias_method :initialize_without_defaults, :initialize
59-
60-
def default_args
61-
{
62-
ssl_cert_store: OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
63-
}
64-
end
65-
66-
def initialize args
67-
initialize_without_defaults default_args.merge(args)
68-
end
69-
end
70-
7153
# @api private
7254
class RestClient::Resource
7355
include Conjur::Escape

lib/conjur/api/authn.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def login username, password, account: Conjur.configuration.account
5050
url_for(:authn_login, account, username, password).get
5151
end
5252

53-
# Exchanges Conjur the API key (refresh token) for an access token. The access token can
53+
# Exchanges Conjur the API key (refresh token) for an access token. The access token can
5454
# then be used to authenticate further API calls.
5555
#
5656
# @param [String] username The username or host id for which we want a token
@@ -65,7 +65,7 @@ def authenticate username, api_key, account: Conjur.configuration.account
6565
JSON.parse url_for(:authn_authenticate, account, username).post(api_key, content_type: 'text/plain')
6666
end
6767

68-
# Obtains an access token from the +authn_local+ service. The access token can
68+
# Obtains an access token from the +authn_local+ service. The access token can
6969
# then be used to authenticate further API calls.
7070
#
7171
# @param [String] username The username or host id for which we want a token
@@ -80,7 +80,7 @@ def authenticate_local username, account: Conjur.configuration.account, expirati
8080
require 'json'
8181
require 'socket'
8282
message = url_for(:authn_authenticate_local, username, account, expiration, cidr)
83-
JSON.parse(UNIXSocket.open(Conjur.configuration.authn_local_socket) {|s| s.puts message; s.gets })
83+
JSON.parse(UNIXSocket.open(Conjur.configuration.authn_local_socket) {|s| s.puts message; s.gets })
8484
end
8585

8686
# Change a user's password. To do this, you must have the user's current password. This does not change or rotate

lib/conjur/api/router/v4.rb

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,27 @@ module V4
88

99
def authn_login account, username, password
1010
verify_account(account)
11-
RestClient::Resource.new(Conjur.configuration.authn_url, user: username, password: password)['users/login']
11+
RestClient::Resource.new(
12+
Conjur.configuration.authn_url,
13+
Conjur.configuration.create_rest_client_options(
14+
user: username,
15+
password: password
16+
)
17+
)['users/login']
1218
end
1319

1420
def authn_authenticate account, username
1521
verify_account(account)
16-
RestClient::Resource.new(Conjur.configuration.authn_url)['users'][fully_escape username]['authenticate']
22+
RestClient::Resource.new(
23+
Conjur.configuration.authn_url,
24+
Conjur.configuration.rest_client_options
25+
)['users'][fully_escape username]['authenticate']
1726
end
1827

1928
# For v4, the authn-local message is the username.
2029
def authn_authenticate_local username, account, expiration, cidr, &block
2130
verify_account(account)
22-
31+
2332
raise "'expiration' is not supported for authn-local v4" if expiration
2433
raise "'cidr' is not supported for authn-local v4" if cidr
2534

@@ -28,36 +37,51 @@ def authn_authenticate_local username, account, expiration, cidr, &block
2837

2938
def authn_rotate_api_key credentials, account, id
3039
verify_account(account)
31-
username = if id.kind == "user"
32-
id.identifier
33-
else
34-
[ id.kind, id.identifier ].join('/')
35-
end
36-
RestClient::Resource.new(Conjur.configuration.authn_url, credentials)['users']["api_key?id=#{username}"]
40+
username = id.kind == "user" ? id.identifier : [id.kind, id.identifier].join('/')
41+
RestClient::Resource.new(
42+
Conjur.configuration.authn_url,
43+
Conjur.configuration.create_rest_client_options(credentials)
44+
)['users']["api_key?id=#{username}"]
3745
end
3846

3947
def authn_rotate_own_api_key account, username, password
4048
verify_account(account)
41-
RestClient::Resource.new(Conjur.configuration.authn_url, user: username, password: password)['users']["api_key"]
49+
RestClient::Resource.new(
50+
Conjur.configuration.authn_url,
51+
Conjur.configuration.create_rest_client_options(user: username, password: password)
52+
)['users']["api_key"]
4253
end
4354

4455
def host_factory_create_host token
4556
http_options = {
4657
headers: { authorization: %Q(Token token="#{token}") }
4758
}
48-
RestClient::Resource.new(Conjur.configuration.core_url, http_options)['host_factories']['hosts']
59+
RestClient::Resource.new(
60+
Conjur.configuration.core_url,
61+
Conjur.configuration.create_rest_client_options(http_options)
62+
)['host_factories']['hosts']
4963
end
5064

5165
def host_factory_create_tokens credentials, id
52-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['host_factories'][id.identifier]['tokens']
66+
RestClient::Resource.new(
67+
Conjur.configuration.core_url,
68+
Conjur.configuration.create_rest_client_options(credentials)
69+
)['host_factories'][id.identifier]['tokens']
5370
end
5471

5572
def host_factory_revoke_token credentials, token
56-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['host_factories']['tokens'][token]
73+
RestClient::Resource.new(
74+
Conjur.configuration.core_url,
75+
Conjur.configuration.create_rest_client_options(credentials)
76+
)['host_factories']['tokens'][token]
5777
end
5878

5979
def resources_resource credentials, id
60-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['authz'][id.account]['resources'][id.kind][id.identifier]
80+
81+
RestClient::Resource.new(
82+
Conjur.configuration.core_url,
83+
Conjur.configuration.create_rest_client_options(credentials)
84+
)['authz'][id.account]['resources'][id.kind][id.identifier]
6185
end
6286

6387
def resources_check credentials, id, privilege, role
@@ -73,47 +97,80 @@ def resources_check credentials, id, privilege, role
7397
end
7498

7599
def resources_permitted_roles credentials, id, privilege
76-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['authz'][id.account]['roles']['allowed_to'][privilege][id.kind][id.identifier]
100+
RestClient::Resource.new(
101+
Conjur.configuration.core_url,
102+
Conjur.configuration.create_rest_client_options(credentials)
103+
)['authz'][id.account]['roles']['allowed_to'][privilege][id.kind][id.identifier]
77104
end
78105

79106
def roles_role credentials, id
80-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['authz'][id.account]['roles'][id.kind][id.identifier]
107+
RestClient::Resource.new(
108+
Conjur.configuration.core_url,
109+
Conjur.configuration.create_rest_client_options(credentials)
110+
)['authz'][id.account]['roles'][id.kind][id.identifier]
81111
end
82112

83113
def secrets_add credentials, id
84114
verify_account(id.account)
85-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['variables'][fully_escape id.identifier]['values']
115+
RestClient::Resource.new(
116+
Conjur.configuration.core_url,
117+
Conjur.configuration.create_rest_client_options(credentials)
118+
)['variables'][fully_escape id.identifier]['values']
86119
end
87120

88121
def variable credentials, id
89122
verify_account(id.account)
90-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['variables'][fully_escape id.identifier]
123+
RestClient::Resource.new(
124+
Conjur.configuration.core_url,
125+
Conjur.configuration.create_rest_client_options(credentials)
126+
)['variables'][fully_escape id.identifier]
91127
end
92128

93129
def secrets_value credentials, id, options
94-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['variables'][fully_escape id.identifier]['value'][options_querystring options]
130+
RestClient::Resource.new(
131+
Conjur.configuration.core_url,
132+
Conjur.configuration.create_rest_client_options(credentials)
133+
)['variables'][fully_escape id.identifier]['value'][options_querystring options]
95134
end
96135

97136
def secrets_values credentials, variable_ids
98137
options = {
99138
vars: Array(variable_ids).map { |v| fully_escape(v.identifier) }.join(',')
100139
}
101-
RestClient::Resource.new(Conjur.configuration.core_url, credentials)['variables']['values'][options_querystring options]
140+
RestClient::Resource.new(
141+
Conjur.configuration.core_url,
142+
Conjur.configuration.create_rest_client_options(credentials)
143+
)['variables']['values'][options_querystring options]
102144
end
103145

104146
def group_attributes credentials, resource, id
105147
verify_account(id.account)
106-
JSON.parse(RestClient::Resource.new(Conjur.configuration.core_url, credentials)['groups'][fully_escape id.identifier].get)
148+
JSON.parse(
149+
RestClient::Resource.new(
150+
Conjur.configuration.core_url,
151+
Conjur.configuration.create_rest_client_options(credentials)
152+
)['groups'][fully_escape id.identifier].get
153+
)
107154
end
108155

109156
def variable_attributes credentials, resource, id
110157
verify_account(id.account)
111-
JSON.parse(RestClient::Resource.new(Conjur.configuration.core_url, credentials)['variables'][fully_escape id.identifier].get)
158+
JSON.parse(
159+
RestClient::Resource.new(
160+
Conjur.configuration.core_url,
161+
Conjur.configuration.create_rest_client_options(credentials)
162+
)['variables'][fully_escape id.identifier].get
163+
)
112164
end
113165

114166
def user_attributes credentials, resource, id
115167
verify_account(id.account)
116-
JSON.parse(RestClient::Resource.new(Conjur.configuration.core_url, credentials)['users'][fully_escape id.identifier].get)
168+
JSON.parse(
169+
RestClient::Resource.new(
170+
Conjur.configuration.core_url,
171+
Conjur.configuration.create_rest_client_options(credentials)
172+
)['users'][fully_escape id.identifier].get
173+
)
117174
end
118175

119176
def parse_group_gidnumber attributes

0 commit comments

Comments
 (0)