From 90b7e971b30039cd67e7201972b4579865f13766 Mon Sep 17 00:00:00 2001 From: Oren Ben Meir Date: Sun, 7 Jun 2020 17:32:29 +0300 Subject: [PATCH] Print Conjur version only on server startup (#1590) * Print Conjur version only on server startup The print that we added didn't print the version only on server startup, and actually printed it for every `conjurctl` command. This commit changes this behaviour so the message is printed only on server startup. * Add changelog entry for printing Conjur version * Add cucumber test for conjurctl retrieve-key * Revert to original environment after each cucumber test Some test manipulate the env so we need to revert to the original one after each scenario. Co-authored-by: John Tuttle --- CHANGELOG.md | 3 +++ app/domain/logs.rb | 5 ----- bin/conjur-cli.rb | 3 +++ config/initializers/status.rb | 2 -- .../features/support/hooks.rb | 13 +++++++++++++ cucumber/api/features/retrieve_api_key.feature | 14 ++++++++++++++ .../features/step_definitions/conjurctl_steps.rb | 14 ++++++++++++++ .../features/step_definitions/environment_steps.rb | 5 +++++ cucumber/api/features/support/hooks.rb | 11 +++++++++++ cucumber/authenticators/features/support/hooks.rb | 13 +++++++++++++ cucumber/policy/features/support/hooks.rb | 13 +++++++++++++ 11 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 cucumber/api/features/retrieve_api_key.feature create mode 100644 cucumber/api/features/step_definitions/conjurctl_steps.rb create mode 100644 cucumber/api/features/step_definitions/environment_steps.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 149ebc3179..85f9e9b6f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased +### Fixed +- The Conjur version is now printed on server startup, after running `conjurctl server` + ([cyberark/conjur#1590](https://github.com/cyberark/conjur/pull/1590)) ## [1.7.1] - 2020-06-03 diff --git a/app/domain/logs.rb b/app/domain/logs.rb index a62dfbf32c..ee421bb4c1 100644 --- a/app/domain/logs.rb +++ b/app/domain/logs.rb @@ -222,11 +222,6 @@ module Util code: "CONJ00023D" ) - ConjurVersionStartup = ::Util::TrackableLogMessageClass.new( - msg: "Conjur v{0-conjur-version} starting up...", - code: "CONJ00037I" - ) - end end end diff --git a/bin/conjur-cli.rb b/bin/conjur-cli.rb index 77db37c7da..f6aec9db0d 100644 --- a/bin/conjur-cli.rb +++ b/bin/conjur-cli.rb @@ -72,6 +72,9 @@ def test_select end Process.fork do + conjur_version = File.read(File.expand_path("../VERSION", File.dirname(__FILE__))).strip + puts "Conjur v#{conjur_version} starting up..." + exec "rails server -p #{options[:port]} -b #{options[:'bind-address']}" end Process.fork do diff --git a/config/initializers/status.rb b/config/initializers/status.rb index 730dc81279..70612c00f2 100644 --- a/config/initializers/status.rb +++ b/config/initializers/status.rb @@ -2,5 +2,3 @@ require 'logs' ENV["CONJUR_VERSION_DISPLAY"] = File.read(File.expand_path("../../VERSION", File.dirname(__FILE__))) - -Rails.logger.info(LogMessages::Util::ConjurVersionStartup.new(ENV["CONJUR_VERSION_DISPLAY"].strip)) diff --git a/cucumber/_authenticators_common/features/support/hooks.rb b/cucumber/_authenticators_common/features/support/hooks.rb index c4d922e03b..8435668100 100644 --- a/cucumber/_authenticators_common/features/support/hooks.rb +++ b/cucumber/_authenticators_common/features/support/hooks.rb @@ -29,4 +29,17 @@ # share this code, and to it the api way (probably) creds.password = 'SEcret12!!!!' creds.save(raise_on_save_failure: true) + + # Save env to revert to it after the test + @env = {} + ENV.each do |key, value| + @env[key] = value + end +end + +After do + # Revert to original env + @env.each do |key, value| + ENV[key] = value + end end diff --git a/cucumber/api/features/retrieve_api_key.feature b/cucumber/api/features/retrieve_api_key.feature new file mode 100644 index 0000000000..228f8e8c06 --- /dev/null +++ b/cucumber/api/features/retrieve_api_key.feature @@ -0,0 +1,14 @@ +Feature: Retrieving an API key with conjurctl + + Background: + # We need to be in production environment to test this to demonstrate a real use-case + Given I set environment variable "RAILS_ENV" to "production" + And I set environment variable "CONJUR_LOG_LEVEL" to "info" + + Scenario: Retrieve an API key + When I retrieve an API key for user "cucumber:user:admin" using conjurctl + Then the API key is correct + + Scenario: Retrieve an API key of a non-existing user fails + When I retrieve an API key for user "cucumber:user:non-existing-user" using conjurctl + Then the stderr includes the error "role does not exist" diff --git a/cucumber/api/features/step_definitions/conjurctl_steps.rb b/cucumber/api/features/step_definitions/conjurctl_steps.rb new file mode 100644 index 0000000000..ba342e9a15 --- /dev/null +++ b/cucumber/api/features/step_definitions/conjurctl_steps.rb @@ -0,0 +1,14 @@ +require 'open3' + +When(/^I retrieve an API key for user "([^"]*)" using conjurctl$/) do |user_id| + command = "conjurctl role retrieve-key #{user_id}" + @conjurctl_stdout, @conjurctl_stderr, = Open3.capture3(command) +end + +Then(/^the API key is correct$/) do + expect(@conjurctl_stdout).to eq("#{Credentials['cucumber:user:admin'].api_key}\n") +end + +Then(/^the stderr includes the error "([^"]*)"$/) do |error| + expect(@conjurctl_stderr).to include(error) +end diff --git a/cucumber/api/features/step_definitions/environment_steps.rb b/cucumber/api/features/step_definitions/environment_steps.rb new file mode 100644 index 0000000000..a54123f01a --- /dev/null +++ b/cucumber/api/features/step_definitions/environment_steps.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Given(/^I set environment variable "([^"]*)" to "([^"]*)"$/) do |variable_name, variable_value| + ENV[variable_name] = variable_value +end diff --git a/cucumber/api/features/support/hooks.rb b/cucumber/api/features/support/hooks.rb index 18ce5a5ee5..56f711ffe7 100644 --- a/cucumber/api/features/support/hooks.rb +++ b/cucumber/api/features/support/hooks.rb @@ -26,10 +26,21 @@ Account.find_or_create_accounts_resource admin_role = Role.create(role_id: "cucumber:user:admin") Credentials.new(role: admin_role).save(raise_on_save_failure: true) + + # Save env to revert to it after the test + @env = {} + ENV.each do |key, value| + @env[key] = value + end end After do FileUtils.remove_dir('cuke_export') if Dir.exists?('cuke_export') + + # Revert to original env + @env.each do |key, value| + ENV[key] = value + end end Before("@logged-in") do diff --git a/cucumber/authenticators/features/support/hooks.rb b/cucumber/authenticators/features/support/hooks.rb index 57ce6f8ad5..7daffa9fc9 100644 --- a/cucumber/authenticators/features/support/hooks.rb +++ b/cucumber/authenticators/features/support/hooks.rb @@ -24,4 +24,17 @@ # this code, and to it the api way (probably) creds.password = 'SEcret12!!!!' creds.save(raise_on_save_failure: true) + + # Save env to revert to it after the test + @env = {} + ENV.each do |key, value| + @env[key] = value + end +end + +After do + # Revert to original env + @env.each do |key, value| + ENV[key] = value + end end diff --git a/cucumber/policy/features/support/hooks.rb b/cucumber/policy/features/support/hooks.rb index 2af2af504f..9bd9c219ef 100644 --- a/cucumber/policy/features/support/hooks.rb +++ b/cucumber/policy/features/support/hooks.rb @@ -37,4 +37,17 @@ # this code, and to it the api way (probably) creds.password = 'SEcret12!!!!' creds.save(raise_on_save_failure: true) + + # Save env to revert to it after the test + @env = {} + ENV.each do |key, value| + @env[key] = value + end +end + +After do + # Revert to original env + @env.each do |key, value| + ENV[key] = value + end end