hybrid_platforms_conductor 33.2.2 → 33.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +49 -0
  3. data/README.md +29 -2
  4. data/docs/config_dsl.md +45 -0
  5. data/docs/plugins.md +1 -0
  6. data/docs/plugins/secrets_reader/keepass.md +62 -0
  7. data/lib/hybrid_platforms_conductor/bitbucket.rb +134 -90
  8. data/lib/hybrid_platforms_conductor/cmd_runner.rb +4 -4
  9. data/lib/hybrid_platforms_conductor/common_config_dsl/bitbucket.rb +12 -44
  10. data/lib/hybrid_platforms_conductor/common_config_dsl/github.rb +9 -31
  11. data/lib/hybrid_platforms_conductor/config.rb +0 -35
  12. data/lib/hybrid_platforms_conductor/confluence.rb +93 -88
  13. data/lib/hybrid_platforms_conductor/connector.rb +1 -1
  14. data/lib/hybrid_platforms_conductor/core_extensions/bundler/without_bundled_env.rb +18 -1
  15. data/lib/hybrid_platforms_conductor/credentials.rb +122 -97
  16. data/lib/hybrid_platforms_conductor/deployer.rb +37 -30
  17. data/lib/hybrid_platforms_conductor/github.rb +39 -0
  18. data/lib/hybrid_platforms_conductor/hpc_plugins/action/bash.rb +1 -1
  19. data/lib/hybrid_platforms_conductor/hpc_plugins/action/remote_bash.rb +27 -17
  20. data/lib/hybrid_platforms_conductor/hpc_plugins/connector/local.rb +4 -2
  21. data/lib/hybrid_platforms_conductor/hpc_plugins/connector/my_connector.rb.sample +1 -1
  22. data/lib/hybrid_platforms_conductor/hpc_plugins/connector/ssh.rb +29 -20
  23. data/lib/hybrid_platforms_conductor/hpc_plugins/platform_handler/serverless_chef.rb +1 -1
  24. data/lib/hybrid_platforms_conductor/hpc_plugins/provisioner/proxmox.rb +5 -3
  25. data/lib/hybrid_platforms_conductor/hpc_plugins/provisioner/proxmox/reserve_proxmox_container +1 -0
  26. data/lib/hybrid_platforms_conductor/hpc_plugins/report/confluence.rb +3 -1
  27. data/lib/hybrid_platforms_conductor/hpc_plugins/secrets_reader/keepass.rb +174 -0
  28. data/lib/hybrid_platforms_conductor/hpc_plugins/secrets_reader/thycotic.rb +3 -1
  29. data/lib/hybrid_platforms_conductor/hpc_plugins/test/bitbucket_conf.rb +4 -1
  30. data/lib/hybrid_platforms_conductor/hpc_plugins/test/github_ci.rb +4 -1
  31. data/lib/hybrid_platforms_conductor/hpc_plugins/test/jenkins_ci_conf.rb +7 -3
  32. data/lib/hybrid_platforms_conductor/hpc_plugins/test/jenkins_ci_masters_ok.rb +8 -4
  33. data/lib/hybrid_platforms_conductor/hpc_plugins/test_report/confluence.rb +3 -1
  34. data/lib/hybrid_platforms_conductor/logger_helpers.rb +24 -1
  35. data/lib/hybrid_platforms_conductor/plugins.rb +1 -0
  36. data/lib/hybrid_platforms_conductor/safe_merge.rb +37 -0
  37. data/lib/hybrid_platforms_conductor/thycotic.rb +80 -75
  38. data/lib/hybrid_platforms_conductor/topographer/plugins/graphviz.rb +5 -3
  39. data/lib/hybrid_platforms_conductor/version.rb +1 -1
  40. data/spec/hybrid_platforms_conductor_test.rb +10 -0
  41. data/spec/hybrid_platforms_conductor_test/api/actions_executor/actions/bash_spec.rb +15 -0
  42. data/spec/hybrid_platforms_conductor_test/api/actions_executor/actions/remote_bash_spec.rb +32 -0
  43. data/spec/hybrid_platforms_conductor_test/api/actions_executor/connectors/local/remote_actions_spec.rb +9 -0
  44. data/spec/hybrid_platforms_conductor_test/api/actions_executor/connectors/ssh/config_dsl_spec.rb +8 -6
  45. data/spec/hybrid_platforms_conductor_test/api/actions_executor/connectors/ssh/remote_actions_spec.rb +38 -0
  46. data/spec/hybrid_platforms_conductor_test/api/cmd_runner_spec.rb +21 -1
  47. data/spec/hybrid_platforms_conductor_test/api/config_spec.rb +48 -72
  48. data/spec/hybrid_platforms_conductor_test/api/credentials_spec.rb +251 -0
  49. data/spec/hybrid_platforms_conductor_test/api/deployer/config_dsl_spec.rb +36 -0
  50. data/spec/hybrid_platforms_conductor_test/api/deployer/secrets_reader_plugins/keepass_spec.rb +680 -0
  51. data/spec/hybrid_platforms_conductor_test/api/deployer/secrets_reader_plugins/thycotic_spec.rb +2 -2
  52. data/spec/hybrid_platforms_conductor_test/api/nodes_handler/cmdbs_plugins_api_spec.rb +2 -2
  53. data/spec/hybrid_platforms_conductor_test/api/platform_handlers/serverless_chef/services_deployment_spec.rb +1 -1
  54. data/spec/hybrid_platforms_conductor_test/api/tests_runner/test_plugins/bitbucket_conf_spec.rb +49 -69
  55. data/spec/hybrid_platforms_conductor_test/api/tests_runner/test_plugins/github_ci_spec.rb +29 -39
  56. data/spec/hybrid_platforms_conductor_test/executables/nodes_to_deploy_spec.rb +21 -15
  57. data/spec/hybrid_platforms_conductor_test/test_connector.rb +2 -2
  58. metadata +188 -139
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90e31431abb4e4924342d8057fbb2d2f54d780975ef7079498045a7897262067
4
- data.tar.gz: 3bfcb046a30115a681d407559f9395766439ada582a0d57070d5bae067776292
3
+ metadata.gz: daf46892dd3b5a79ff0e54f16d6881b80519e8fe926e9952ad7faafd1706c31e
4
+ data.tar.gz: 84f9ee06afb8141f140a8754d0961217ceb14b3f8d2d1d2577792be420d42aa2
5
5
  SHA512:
6
- metadata.gz: c532fc150a6ac25db61a70de51b88332728cb93b27f1275fda6cf15b0fce03d2fce9b90f7ea1c65134c0475e10708d075235e9abc2ec5710bf8e4823a98dc2e7
7
- data.tar.gz: d081b32a30c854f0445f98669ba417ee80256f65558b1d274e476b20bd6a0c35133f0f711adae65c9e118a027136caaffce91cd179a2202ae32623b2469d6b54
6
+ metadata.gz: 7b1355694d9c7dbc5b2d2f9f6555972c25033d11bd07b14dfc728c94b2965ac1a852c91cb6ed4f20a26bc41cceb12f437a8b599177331cbf544e38ebc387cd4a
7
+ data.tar.gz: 8636507ef5724bd9f0b3ffbb1f08c3efd1a48d39fe7161a4421350b4a03a9afff279b5522175418742ba87f7daca4dcdb4382e10f3283c39f9816eceba04bdc3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,52 @@
1
+ # [v33.5.0](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.4.0...v33.5.0) (2021-07-07 11:03:01)
2
+
3
+ ### Features
4
+
5
+ * [[Feature] [Security] [#80] Use SecretString for credentials to avoid passwords and secrets accidental leakage](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/0db5f529e539b81f86b9f618a63e62bdbd58cb38)
6
+
7
+ # [v33.4.0](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.3.0...v33.4.0) (2021-07-05 13:24:27)
8
+
9
+ ### Features
10
+
11
+ * [[Feature] [#83] Credentials can now be given through config DSL for better security and control](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/9dd40e82b4c71bea9686abd828adb4359f5bebb2)
12
+
13
+ # [v33.3.0](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.2.4...v33.3.0) (2021-07-02 17:20:58)
14
+
15
+ ## Global changes
16
+ ### Patches
17
+
18
+ * [[Feature(secrets_reader_keepass)] [#79] Add Keepass secrets reader plugin to get secrets from KeePass databases](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/7ace48653a74f03ed535ee6b41b21242a6454ff3)
19
+
20
+ ## Changes for secrets_reader_keepass
21
+ ### Features
22
+
23
+ * [[Feature(secrets_reader_keepass)] [#79] Add Keepass secrets reader plugin to get secrets from KeePass databases](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/7ace48653a74f03ed535ee6b41b21242a6454ff3)
24
+
25
+ # [v33.2.4](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.2.3...v33.2.4) (2021-06-23 15:14:20)
26
+
27
+ ## Global changes
28
+ ### Patches
29
+
30
+ * [[Hotfix(platform_handler_serverless_chef)] Forward environment in sudo commands](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/fd3b58875665c29dd071b5af2055eab0c45c0974)
31
+ * [[Hotfix] Fixed unbundled environment not cleaned + Moved deployer config DSL in deployer.rb](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/2bce6dbc31d98b27f196ed646eb9aa669b0f9a86)
32
+
33
+ ## Changes for platform_handler_serverless_chef
34
+ ### Patches
35
+
36
+ * [[Hotfix(platform_handler_serverless_chef)] Forward environment in sudo commands](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/fd3b58875665c29dd071b5af2055eab0c45c0974)
37
+
38
+ # [v33.2.3](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.2.2...v33.2.3) (2021-06-23 13:45:56)
39
+
40
+ ## Global changes
41
+ ### Patches
42
+
43
+ * [[Hotfix(provisioner_proxmox)] Add missing require in synchronization script](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/7a6c71595789c9180e1d95323fc5cf5051f2e2cd)
44
+
45
+ ## Changes for provisioner_proxmox
46
+ ### Patches
47
+
48
+ * [[Hotfix(provisioner_proxmox)] Add missing require in synchronization script](https://github.com/sweet-delights/hybrid-platforms-conductor/commit/7a6c71595789c9180e1d95323fc5cf5051f2e2cd)
49
+
1
50
  # [v33.2.2](https://github.com/sweet-delights/hybrid-platforms-conductor/compare/v33.2.1...v33.2.2) (2021-06-21 12:41:35)
2
51
 
3
52
  ## Global changes
data/README.md CHANGED
@@ -304,12 +304,39 @@ See [the executables list](docs/executables.md) for more details.
304
304
  # Credentials
305
305
 
306
306
  Some tools or tests require authentication using user/password to an external resource. Examples of such tools are Bitbucket, Thycotic, Confluence...
307
- Credentials can be given using either environment variables or by parsing the user's `.netrc` file.
307
+ Credentials can be given 3 different ways:
308
+ * from the configuration file `hpc_config.rb`,
309
+ * from environment variables,
310
+ * from the user's `.netrc` file.
308
311
 
309
- In case a process needs a credential that has not been set, a warning message will be output so that the user knows which credential is missing, and eventually for which URL.
312
+ In case a process needs a credential that has not been set, a warning message will be output so that the user knows which credential is missing, and eventually for which resource (URL, file...).
310
313
 
311
314
  Following sub-sections explain the different ways of setting such credentials.
312
315
 
316
+ ## Configuration
317
+
318
+ The [`credentials_for` config DSL method](docs/config_dsl.md#credentials_for) can be used to define the credentials for a given credential ID and eventual resource.
319
+ The way to do it is to provide a callback that will be called only when a credential is needed, and the credentials are given to a requester object. This way the life-cycle of the secret can be completely controlled, and clean-up can be done to ensure no vulnerabilities are staying after usage.
320
+
321
+ Example:
322
+ ```ruby
323
+ # Simple case
324
+ credentials_for(:bitbucket) do |resource, requester|
325
+ requester.call 'my_bitbucket_name', 'my_bitbucket_PaSsWoRd'
326
+ end
327
+
328
+ # More secure case, handling user input and memory clean-up after usage
329
+ credentials_for(:bitbucket) do |resource, requester|
330
+ puts 'Input Bitbucket password...'
331
+ password = ''
332
+ $stdin.noecho { |io| io.sysread(256, password) }
333
+ password.chomp!
334
+ SecretString.protect(password) do |secret_password|
335
+ requester.call 'my_bitbucket_name', secret_password
336
+ end
337
+ end
338
+ ```
339
+
313
340
  ## Environment variables
314
341
 
315
342
  Environment variables used for credentials are always named following this convention: `hpc_user_for_<credential_id>` and `hpc_password_for_<credential_id>`.
data/docs/config_dsl.md CHANGED
@@ -13,6 +13,7 @@ This DSL can also be completed by plugins. Check [the plugins documentations](pl
13
13
  * [`hybrid_platforms_dir`](#hybrid_platforms_dir)
14
14
  * [`tests_provisioner`](#tests_provisioner)
15
15
  * [`expect_tests_to_fail`](#expect_tests_to_fail)
16
+ * [`credentials_for`](#credentials_for)
16
17
  * [`read_secrets_from`](#read_secrets_from)
17
18
  * [`send_logs_to`](#send_logs_to)
18
19
  * [`retry_deploy_for_errors_on_stdout`](#retry_deploy_for_errors_on_stdout)
@@ -202,6 +203,50 @@ for_nodes('/tst/') do
202
203
  end
203
204
  ```
204
205
 
206
+ <a name="credentials_for"></a>
207
+ ## `credentials_for`
208
+
209
+ Set the credentials to be used (user, password) for a given credential ID and an optional resources selection.
210
+
211
+ Credentials can be used by any plugin (using the [`with_credentials_for`](/lib/hybrid_platforms_conductor/credentials.rb) method) and are used by various processes in Hybrid Platforms Conductor. See each [plugin's documentation](plugins.md) to know which plugin uses which credential.
212
+
213
+ It takes the following parameters:
214
+ * the credential ID (as a Symbol),
215
+ * an optional resource specification (can be a String for a complete resource name, or a Regexp matching resources' names),
216
+ * a code block that will be called back by any process needing credentials matching the ID and resource specification.
217
+ The code block will be given both the resource name being accessed, and a requester object that needs to be given the corresponding user and password. When the requester finishes running, the credentials are not needed anymore and should be cleaned from memory to avoid vulnerabilities.
218
+
219
+ A secure way to five the password is to use a [`SecretString`](https://github.com/Muriel-Salvan/secret_string) class that will give the following guarantees:
220
+ * Avoid the password to leak inadvertently in logs, on-screen, files...
221
+ * Clear the password from memory as soon as it is not needed anymore.
222
+
223
+ Examples:
224
+ ```ruby
225
+ # Using an environment variable as a password
226
+ credentials_for(:github) do |resource, requester|
227
+ requester.call 'MyUserName', ENV['MY_GITHUB_PASSWORD']
228
+ end
229
+
230
+ # Using user input and cleaning memory
231
+ credentials_for(:github) do |resource, requester|
232
+ puts 'Input Github password...'
233
+ password = ''
234
+ $stdin.noecho { |io| io.sysread(256, password) }
235
+ password.chomp!
236
+ SecretString.protect(password) do |secret_password|
237
+ requester.call 'MyUserName', secret_password
238
+ end
239
+ end
240
+
241
+ # Defining different credentials based on the resource being accessed
242
+ credentials_for(:github, resource: %r{github.com/my-projects}) do |resource, requester|
243
+ requester.call 'MyUserName', 'MyPassword'
244
+ end
245
+ credentials_for(:github, resource: %r{github.com/company}) do |resource, requester|
246
+ requester.call 'CompanyUserName', 'CompanyPassword'
247
+ end
248
+ ```
249
+
205
250
  <a name="read_secrets_from"></a>
206
251
  ## `read_secrets_from`
207
252
 
data/docs/plugins.md CHANGED
@@ -196,6 +196,7 @@ Check the [sample plugin file](../lib/hybrid_platforms_conductor/hpc_plugins/sec
196
196
 
197
197
  Plugins shipped by default:
198
198
  * [`cli`](plugins/secrets_reader/cli.md)
199
+ * [`keepass`](plugins/secrets_reader/keepass.md)
199
200
  * [`thycotic`](plugins/secrets_reader/thycotic.md)
200
201
 
201
202
  <a name="test"></a>
@@ -0,0 +1,62 @@
1
+ # Secrets reader plugin: `keepass`
2
+
3
+ The `keepass` secrets reader plugin retrieves secrets from [KeePass](https://keepass.info/) databases, using an actual KeePass installation with the [KPScript plugin](https://keepass.info/plugins.html#kpscript).
4
+
5
+ It is configured by giving the KPScript command-line (using `use_kpscript_from` config DSL method), the KeePass databases to be read (using `secrets_from_keepass` config DSL method) and uses the `keepass` credential ID to authenticate along with extra environment variables for eventual key files or encrypted passwords.
6
+
7
+ ## Config DSL extension
8
+
9
+ ### `use_kpscript_from`
10
+
11
+ Provide the KPScript command-line to be used. If KPScript is already in your path, using `KPScript.exe` or `kpscript` should be enough, otherwise the full path to the command-line will be needed. On Windows it is needed to also include double quotes if the path contains spaces (like `"C:\Program Files\KeePass\KPScript.exe"`).
12
+
13
+ It takes a simple `String` as parameter to get the command line.
14
+
15
+ Example:
16
+ ```ruby
17
+ use_kpscript_from '/path/to/kpscript'
18
+ ```
19
+
20
+ ### `secrets_from_keepass`
21
+
22
+ Define a KeePass database to read secrets from.
23
+ A base group path of the KeePass database can also be specified to only read secrets from this group path.
24
+
25
+ All entries, attachments and sub-groups from the base group will be read as secrets.
26
+
27
+ Can be applied to subset of nodes using the [`for_nodes` DSL method](/docs/config_dsl.md#for_nodes).
28
+
29
+ It takes the following parameters:
30
+ * **database** (`String`): Database file path.
31
+ * **group_path** (`Array<String>`): Group path to extract from [default: `[]`].
32
+
33
+ Example:
34
+ ```ruby
35
+ secrets_from_keepass(
36
+ database: '/path/to/database.kdbx',
37
+ group_path: %w[Secrets Automation]
38
+ )
39
+ ```
40
+
41
+ ## Used credentials
42
+
43
+ | Credential | Usage
44
+ | --- | --- |
45
+ | `keepass` | Used to get the password to the database. No need to be set if the database opens without password. |
46
+
47
+ ## Used Metadata
48
+
49
+ | Metadata | Type | Usage
50
+ | --- | --- | --- |
51
+
52
+ ## Used environment variables
53
+
54
+ | Variable | Usage
55
+ | --- | --- |
56
+ | `hpc_key_file_for_keepass` | Optional path to the key file needed to open the database |
57
+ | `hpc_password_enc_for_keepass` | Optional encrypted password needed to open the database |
58
+
59
+ ## External tools dependencies
60
+
61
+ * [KeePass](https://keepass.info/) to open databases.
62
+ * [KPScript KeePass plugin](https://keepass.info/plugins.html#kpscript) to query KeePass API.
@@ -7,116 +7,160 @@ require 'hybrid_platforms_conductor/logger_helpers'
7
7
 
8
8
  module HybridPlatformsConductor
9
9
 
10
- # Object used to access Bitbucket API
11
- class Bitbucket
10
+ # Mixin used to access Bitbucket API
11
+ module Bitbucket
12
12
 
13
- include LoggerHelpers
13
+ include Credentials
14
14
 
15
15
  # Provide a Bitbucket connector, and make sure the password is being cleaned when exiting.
16
16
  #
17
17
  # Parameters::
18
18
  # * *bitbucket_url* (String): The Bitbucket URL
19
- # * *logger* (Logger): Logger to be used
20
- # * *logger_stderr* (Logger): Logger to be used for stderr
21
19
  # * Proc: Code called with the Bitbucket instance.
22
- # * *bitbucket* (Bitbucket): The Bitbucket instance to use.
23
- def self.with_bitbucket(bitbucket_url, logger, logger_stderr)
24
- Credentials.with_credentials_for(:bitbucket, logger, logger_stderr, url: bitbucket_url) do |bitbucket_user, bitbucket_password|
25
- yield Bitbucket.new(bitbucket_url, bitbucket_user, bitbucket_password, logger: logger, logger_stderr: logger_stderr)
20
+ # * *bitbucket* (BitbucketApi): The Bitbucket instance to use.
21
+ def with_bitbucket(bitbucket_url)
22
+ with_credentials_for(:bitbucket, resource: bitbucket_url) do |bitbucket_user, bitbucket_password|
23
+ yield BitbucketApi.new(bitbucket_url, bitbucket_user, bitbucket_password, logger: @logger, logger_stderr: @logger_stderr)
26
24
  end
27
25
  end
28
26
 
29
- # The Bitbucket URL
30
- # String
31
- attr_reader :bitbucket_url
32
-
33
- # Constructor
27
+ # Iterate over each Bitbucket repository
34
28
  #
35
29
  # Parameters::
36
- # * *bitbucket_url* (String): The Bitbucket URL
37
- # * *bitbucket_user_name* (String): Bitbucket user name to be used when querying the API
38
- # * *bitbucket_password* (String): Bitbucket password to be used when querying the API
39
- # * *logger* (Logger): Logger to be used [default = Logger.new(STDOUT)]
40
- # * *logger_stderr* (Logger): Logger to be used for stderr [default = Logger.new(STDERR)]
41
- def initialize(bitbucket_url, bitbucket_user_name, bitbucket_password, logger: Logger.new($stdout), logger_stderr: Logger.new($stderr))
42
- init_loggers(logger, logger_stderr)
43
- @bitbucket_url = bitbucket_url
44
- @bitbucket_user_name = bitbucket_user_name
45
- @bitbucket_password = bitbucket_password
30
+ # * Proc: Code called for each Bitbucket repository:
31
+ # * Parameters::
32
+ # * *bitbucket* (Bitbucket): The Bitbucket instance used to query the API for this repository
33
+ # * *repo_info* (Hash<Symbol, Object>): The repository info:
34
+ # * *name* (String): Repository name.
35
+ # * *project* (String): Project name.
36
+ # * *url* (String): Project Git URL.
37
+ # * *jenkins_ci_url* (String or nil): Corresponding Jenkins CI URL, or nil if none.
38
+ # * *checks* (Hash<Symbol, Object>): Checks to be performed on this repository:
39
+ # * *branch_permissions* (Array< Hash<Symbol, Object> >): List of branch permissions to check [optional]
40
+ # * *type* (String): Type of branch permissions to check. Examples of values are 'fast-forward-only', 'no-deletes', 'pull-request-only'.
41
+ # * *branch* (String): Branch on which those permissions apply.
42
+ # * *exempted_users* (Array<String>): List of exempted users for this permission [default: []]
43
+ # * *exempted_groups* (Array<String>): List of exempted groups for this permission [default: []]
44
+ # * *exempted_keys* (Array<String>): List of exempted access keys for this permission [default: []]
45
+ # * *pr_settings* (Hash<Symbol, Object>): PR specific settings to check [optional]
46
+ # * *required_approvers* (Integer): Number of required approvers [optional]
47
+ # * *required_builds* (Integer): Number of required successful builds [optional]
48
+ # * *default_merge_strategy* (String): Name of the default merge strategy. Example: 'rebase-no-ff' [optional]
49
+ # * *mandatory_default_reviewers* (Array<String>): List of mandatory reviewers to check [default: []]
50
+ def for_each_bitbucket_repo
51
+ @config.known_bitbucket_repos.each do |bitbucket_repo_info|
52
+ with_bitbucket(bitbucket_repo_info[:url]) do |bitbucket|
53
+ (bitbucket_repo_info[:repos] == :all ? bitbucket.repos(bitbucket_repo_info[:project])['values'].map { |repo_info| repo_info['slug'] } : bitbucket_repo_info[:repos]).each do |name|
54
+ yield bitbucket, {
55
+ name: name,
56
+ project: bitbucket_repo_info[:project],
57
+ url: "#{bitbucket_repo_info[:url]}/scm/#{bitbucket_repo_info[:project].downcase}/#{name}.git",
58
+ jenkins_ci_url: bitbucket_repo_info[:jenkins_ci_url].nil? ? nil : "#{bitbucket_repo_info[:jenkins_ci_url]}/job/#{name}",
59
+ checks: bitbucket_repo_info[:checks]
60
+ }
61
+ end
62
+ end
63
+ end
46
64
  end
47
65
 
48
- # Get the repositories of a given project.
49
- # Limit to 1000 results max.
50
- #
51
- # Parameters::
52
- # * *project* (String): Project name
53
- # Result::
54
- # * Object: Corresponding JSON
55
- def repos(project)
56
- get_api("projects/#{project}/repos?limit=1000")
57
- end
66
+ # Provide an API to Bitbucket
67
+ class BitbucketApi
58
68
 
59
- # Get the PR settings of a given repository
60
- #
61
- # Parameters::
62
- # * *project* (String): Project name
63
- # * *repo* (String): Repository name
64
- # Result::
65
- # * Object: Corresponding JSON
66
- def settings_pr(project, repo)
67
- get_api("projects/#{project}/repos/#{repo}/settings/pull-requests")
68
- end
69
+ include LoggerHelpers
69
70
 
70
- # Get the default reviewers of a given repository
71
- #
72
- # Parameters::
73
- # * *project* (String): Project name
74
- # * *repo* (String): Repository name
75
- # Result::
76
- # * Object: Corresponding JSON
77
- def default_reviewers(project, repo)
78
- get_api("projects/#{project}/repos/#{repo}/conditions", api_domain: 'default-reviewers')
79
- end
71
+ # The Bitbucket URL
72
+ # String
73
+ attr_reader :bitbucket_url
80
74
 
81
- # Get the branch permissions of a given repository
82
- #
83
- # Parameters::
84
- # * *project* (String): Project name
85
- # * *repo* (String): Repository name
86
- # Result::
87
- # * Object: Corresponding JSON
88
- def branch_permissions(project, repo)
89
- # Put 3 retries here as the Bitbucket installation has a very unstable API 2.0 and often returns random 401 errors.
90
- get_api("projects/#{project}/repos/#{repo}/restrictions", api_domain: 'branch-permissions', api_version: '2.0', retries: 3)
91
- end
75
+ # Constructor
76
+ #
77
+ # Parameters::
78
+ # * *bitbucket_url* (String): The Bitbucket URL
79
+ # * *bitbucket_user_name* (String): Bitbucket user name to be used when querying the API
80
+ # * *bitbucket_password* (SecretString): Bitbucket password to be used when querying the API
81
+ # * *logger* (Logger): Logger to be used [default = Logger.new(STDOUT)]
82
+ # * *logger_stderr* (Logger): Logger to be used for stderr [default = Logger.new(STDERR)]
83
+ def initialize(bitbucket_url, bitbucket_user_name, bitbucket_password, logger: Logger.new($stdout), logger_stderr: Logger.new($stderr))
84
+ init_loggers(logger, logger_stderr)
85
+ @bitbucket_url = bitbucket_url
86
+ @bitbucket_user_name = bitbucket_user_name
87
+ @bitbucket_password = bitbucket_password
88
+ end
92
89
 
93
- # Issue an HTTP get on the API.
94
- # Handle authentication.
95
- #
96
- # Parameters::
97
- # * *path* (String): API path to access
98
- # * *api_domain* (String): API domain to access [default: 'api']
99
- # * *api_version* (String): API version to access [default: '1.0']
100
- # * *retries* (Integer): Number of retries in case of failures [default: 0]
101
- # Result::
102
- # * Object: Returned JSON
103
- def get_api(path, api_domain: 'api', api_version: '1.0', retries: 0)
104
- api_url = "#{@bitbucket_url}/rest/#{api_domain}/#{api_version}/#{path}"
105
- log_debug "Call Bitbucket API #{@bitbucket_user_name}@#{api_url}..."
106
- http_response = nil
107
- loop do
108
- begin
109
- http_response = URI.parse(api_url).open(http_basic_authentication: [@bitbucket_user_name, @bitbucket_password])
110
- rescue
111
- raise if retries.zero?
112
-
113
- log_warn "Got error #{$ERROR_INFO} on #{@bitbucket_user_name}@#{api_url}. Will retry #{retries} times..."
114
- retries -= 1
115
- sleep 1
90
+ # Get the repositories of a given project.
91
+ # Limit to 1000 results max.
92
+ #
93
+ # Parameters::
94
+ # * *project* (String): Project name
95
+ # Result::
96
+ # * Object: Corresponding JSON
97
+ def repos(project)
98
+ get_api("projects/#{project}/repos?limit=1000")
99
+ end
100
+
101
+ # Get the PR settings of a given repository
102
+ #
103
+ # Parameters::
104
+ # * *project* (String): Project name
105
+ # * *repo* (String): Repository name
106
+ # Result::
107
+ # * Object: Corresponding JSON
108
+ def settings_pr(project, repo)
109
+ get_api("projects/#{project}/repos/#{repo}/settings/pull-requests")
110
+ end
111
+
112
+ # Get the default reviewers of a given repository
113
+ #
114
+ # Parameters::
115
+ # * *project* (String): Project name
116
+ # * *repo* (String): Repository name
117
+ # Result::
118
+ # * Object: Corresponding JSON
119
+ def default_reviewers(project, repo)
120
+ get_api("projects/#{project}/repos/#{repo}/conditions", api_domain: 'default-reviewers')
121
+ end
122
+
123
+ # Get the branch permissions of a given repository
124
+ #
125
+ # Parameters::
126
+ # * *project* (String): Project name
127
+ # * *repo* (String): Repository name
128
+ # Result::
129
+ # * Object: Corresponding JSON
130
+ def branch_permissions(project, repo)
131
+ # Put 3 retries here as the Bitbucket installation has a very unstable API 2.0 and often returns random 401 errors.
132
+ get_api("projects/#{project}/repos/#{repo}/restrictions", api_domain: 'branch-permissions', api_version: '2.0', retries: 3)
133
+ end
134
+
135
+ # Issue an HTTP get on the API.
136
+ # Handle authentication.
137
+ #
138
+ # Parameters::
139
+ # * *path* (String): API path to access
140
+ # * *api_domain* (String): API domain to access [default: 'api']
141
+ # * *api_version* (String): API version to access [default: '1.0']
142
+ # * *retries* (Integer): Number of retries in case of failures [default: 0]
143
+ # Result::
144
+ # * Object: Returned JSON
145
+ def get_api(path, api_domain: 'api', api_version: '1.0', retries: 0)
146
+ api_url = "#{@bitbucket_url}/rest/#{api_domain}/#{api_version}/#{path}"
147
+ log_debug "Call Bitbucket API #{@bitbucket_user_name}@#{api_url}..."
148
+ http_response = nil
149
+ loop do
150
+ begin
151
+ http_response = URI.parse(api_url).open(http_basic_authentication: [@bitbucket_user_name, @bitbucket_password&.to_unprotected])
152
+ rescue
153
+ raise if retries.zero?
154
+
155
+ log_warn "Got error #{$ERROR_INFO} on #{@bitbucket_user_name}@#{api_url}. Will retry #{retries} times..."
156
+ retries -= 1
157
+ sleep 1
158
+ end
159
+ break unless http_response.nil?
116
160
  end
117
- break unless http_response.nil?
161
+ JSON.parse(http_response.read)
118
162
  end
119
- JSON.parse(http_response.read)
163
+
120
164
  end
121
165
 
122
166
  end