-
Notifications
You must be signed in to change notification settings - Fork 56
(PE-39411) Add descriptive error during infrastructure upgrade when rbac token is invalid #514
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
Merged
jhbuchanan45
merged 5 commits into
main
from
PE-39411-peadm-check-for-a-valid-rbac-token
Oct 15, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b9170ef
(PE-39411) Rewrite infra upgrade error to be more useful. Add validat…
jhbuchanan45 b208aff
(PE-39411) Validate RBAC token on upgrade if required for compiler up…
jhbuchanan45 8db17e4
(maint) Fix lint issues
jhbuchanan45 c13c900
(maint) Regenerate references.md
jhbuchanan45 43bd9ab
Update tasks/validate_rbac_token.rb
jhbuchanan45 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "description": "Check an RBAC token stored in a file is valid", | ||
| "parameters": { | ||
| "token_file": { | ||
| "type": "Optional[String]", | ||
| "description": "The path to the token file to use" | ||
| } | ||
| }, | ||
| "input_method": "stdin", | ||
| "implementations": [ | ||
| {"name": "validate_rbac_token.rb"} | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #!/opt/puppetlabs/puppet/bin/ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'uri' | ||
| require 'net/https' | ||
| require 'json' | ||
| require 'etc' | ||
| require 'puppet' | ||
|
|
||
| # Class to check an rbac token is valid | ||
| class ValidateRbacToken | ||
| def initialize(params) | ||
| @token_file = params['token_file'] | ||
| end | ||
|
|
||
| def execute! | ||
| token_file = @token_file || File.join(Etc.getpwuid.dir, '.puppetlabs', 'token') | ||
|
|
||
| uri = URI("https://#{Puppet.settings[:certname]}:4433/rbac-api/v2/auth/token/authenticate") | ||
| https = https_object(uri: uri) | ||
| request = request_object(token_file: token_file) | ||
|
|
||
| resp = https.request(request) | ||
|
|
||
| if resp.code == '200' | ||
| puts 'RBAC token is valid' | ||
| exit 0 | ||
| else | ||
| body = JSON.parse(resp.body) | ||
| case resp.code | ||
| when '401', '403' | ||
| puts "#{resp.code} #{body['kind']}: \nCheck your API token at #{token_file}. \ | ||
| An alternate token file can be specified using the token_file param. \n\n\ | ||
| See https://www.puppet.com/docs/pe/latest/rbac_token_auth_intro for more details. \n" | ||
| else | ||
| puts "Error validating token: #{resp.code} #{body['kind']}" | ||
| puts body['msg'] | ||
| end | ||
|
|
||
| exit 1 | ||
| end | ||
| end | ||
|
|
||
| def request_object(token_file:) | ||
| token = File.read(token_file) | ||
| body = { | ||
| 'token' => token.chomp, | ||
| 'update_last_activity?' => false, | ||
| }.to_json | ||
|
|
||
| request = Net::HTTP::Post.new('/rbac-api/v2/auth/token/authenticate') | ||
| request['Content-Type'] = 'application/json' | ||
| request.body = body | ||
|
|
||
| request | ||
| end | ||
|
|
||
| def https_object(uri:) | ||
| https = Net::HTTP.new(uri.host, uri.port) | ||
| https.use_ssl = true | ||
| https.cert = OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert])) | ||
| https.key = OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey])) | ||
| https.verify_mode = OpenSSL::SSL::VERIFY_PEER | ||
| https.ca_file = Puppet.settings[:localcacert] | ||
|
|
||
| https | ||
| end | ||
| end | ||
|
|
||
| # Run the task unless an environment flag has been set, signaling not to. The | ||
| # environment flag is used to disable auto-execution and enable Ruby unit | ||
| # testing of this task. | ||
| unless ENV['RSPEC_UNIT_TEST_MODE'] | ||
| Puppet.initialize_settings | ||
| validate = ValidateRbacToken.new(JSON.parse(STDIN.read)) | ||
| validate.execute! | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.