Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register_to_file: Support Sensitive regtoken #164

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ gitlab_ci_runner::runner { 'testrunner':
}
```

#### `gitlab_ci_runner::register_to_file(String[1] $url, String[1] $regtoken, String[1] $runner_name, Optional[Hash] $additional_options, Optional[Optional[String[1]]] $proxy, Optional[Optional[String[1]]] $ca_file)`
#### `gitlab_ci_runner::register_to_file(String[1] $url, Variant[String[1], Sensitive[String[1]]] $regtoken, String[1] $runner_name, Optional[Hash] $additional_options, Optional[Optional[String[1]]] $proxy, Optional[Optional[String[1]]] $ca_file)`

A function that registers a Gitlab runner on a Gitlab instance, if it doesn't already exist,
_and_ saves the retrieved authentication token to a file. This is helpful for Deferred functions.
Expand Down Expand Up @@ -533,7 +533,7 @@ The url to your Gitlab instance. Please only provide the host part (e.g https://

##### `regtoken`

Data type: `String[1]`
Data type: `Variant[String[1], Sensitive[String[1]]]`

Registration token.

Expand Down
7 changes: 6 additions & 1 deletion lib/puppet/functions/gitlab_ci_runner/register_to_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
dispatch :register_to_file do
# We use only core data types because others aren't synced to the agent.
param 'String[1]', :url
param 'String[1]', :regtoken
param 'Variant[String[1], Sensitive[String[1]]]', :regtoken
param 'String[1]', :runner_name
optional_param 'Hash', :additional_options
optional_param 'Optional[String[1]]', :proxy
Expand All @@ -46,6 +46,11 @@ def register_to_file(url, regtoken, runner_name, additional_options = {}, proxy
Puppet.warning('Unable to register gitlab runner at this time as the specified `ca_file` does not exist (yet). If puppet is managing this file, the next run should complete the registration process.')
return 'Specified CA file doesn\'t exist, not attempting to create authtoken'
end

# If this is a Sensitive value it will be unwrapped, otherwise the String
# will be returned unmodified.
regtoken = call_function('unwrap', regtoken)

authtoken = PuppetX::Gitlab::Runner.register(url, additional_options.merge('token' => regtoken), proxy, ca_file)['token']

# If this function is used as a Deferred function the Gitlab Runner config dir
Expand Down
8 changes: 8 additions & 0 deletions spec/functions/register_to_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@

it { is_expected.to run.with_params(url, regtoken, runner_name, {}, nil, '/path/to/ca_file').and_return('Specified CA file doesn\'t exist, not attempting to create authtoken') }
end

context 'with sensitive token value' do
before do
allow(PuppetX::Gitlab::Runner).to receive(:register).with(url, { 'token' => regtoken }, nil, '/tmp').and_return(return_hash)
end

it { is_expected.to run.with_params(url, sensitive(regtoken), runner_name, {}, nil, '/tmp').and_return(return_hash['token']) }
end
end

context 'noop does not register runner and returns dummy token' do
Expand Down
Loading