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

Enabled authenticators can be configured using configuration file #2217

Merged
merged 5 commits into from
May 27, 2021

Conversation

h-artzi
Copy link
Contributor

@h-artzi h-artzi commented May 26, 2021

What does this PR do?

Add authenticators to Conjur Config

What ticket does this PR close?

Resolves #2173

Checklists

Change log

  • The CHANGELOG has been updated, or
  • This PR does not include user-facing changes and doesn't require a CHANGELOG update

Test coverage

  • This PR includes new unit and integration tests to go with the code changes, or
  • The changes in this PR do not require tests

Documentation

  • Docs (e.g. READMEs) were updated in this PR, and/or there is a follow-on issue to update docs, or
  • This PR does not require updating any documentation

API Changes

  • The OpenAPI spec has been updated to meet new API changes (or an issue has been opened), or
  • The changes in this PR do not affect the Conjur API

@h-artzi h-artzi requested a review from a team as a code owner May 26, 2021 20:41
CHANGELOG.md Outdated
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean this up.

CHANGELOG.md Outdated
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, the
CONJUR_AUTHENTICATORS environment variable, or the authenticator allowlist
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing spaces

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean this up.

CHANGELOG.md Outdated
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lists should be surrounded by blank lines

Copy link
Contributor

@micahlee micahlee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I left a couple of small comments for you. Would you also mind updating the PR title to something descriptive?

Thanks!

lib/conjur/conjur_config.rb Outdated Show resolved Hide resolved
CHANGELOG.md Outdated
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean this up.

CHANGELOG.md Outdated
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, the
CONJUR_AUTHENTICATORS environment variable, or the authenticator allowlist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clean this up.

@h-artzi h-artzi changed the title 2173 authenticators Enabled authenticators can be configured using configuration file May 26, 2021
def env_enabled_authenticators(env)
authenticators = env["CONJUR_AUTHENTICATORS"]
authenticators.present? ? authenticators.split(',') : nil
def env_enabled_authenticators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call this configured_authenticators now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configured_authenticators is already used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, okay. In that case (and knowing that we're going to tear out the DB-configured authenticators sooni-ish) I would get rid of this helper method and just build the logic into the enabled_authenticators method.

authenticators = env["CONJUR_AUTHENTICATORS"]
authenticators.present? ? authenticators.split(',') : nil
def env_enabled_authenticators
authenticators = Conjur::ConjurConfig.new.authenticators.uniq
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Rails.application.config.conjur_config.authenticators within the application code. It gets loaded once when the app starts instead of loading configuration fresh each time ConjurConfig is instantiated.

@@ -32,7 +34,7 @@ class ConjurConfig < Anyway::Config

# Get attribute sources without including attribute values
def attribute_sources
to_source_trace.map { |k,v| [ k.to_sym, v[:source][:type] ] }.to_h
to_source_trace.map { |k, v| [ k.to_sym, v[:source][:type] ] }.to_h
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I had it like this originally but CodeClimate complained.

expect(Conjur::ConjurConfig.new.attribute_sources[:trusted_proxies]).
to eq(:defaults)
expect(Conjur::ConjurConfig.new.attribute_sources[:trusted_proxies])
.to eq(:defaults)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use trailing dots more often than leading so can we change these back?

Copy link
Contributor

@jonahx jonahx May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtuttle Dissenting vote :)

Consider:

        Resource
          .where(identifier.like("#{AUTHN_RESOURCE_PREFIX}%"))
          .where(kind => "webservice")
          .select_map(identifier)
          .map { |id| id[%r{^conjur/(authn(?:-[^/]+)?(?:/[^/]+)?)$}, 1] } # filter out nested status webservice
          .compact
          .push(::Authentication::Common.default_authenticator_name)

leading dot does two things:

  1. Makes clear at a glance that we're dealing with a continuation (otherwise, if you don't notice the dot at the end of the previous line, it could be a new method call on self)
  2. Makes cut and paste more ergonomic (otherwise the final item is a special case).

Copy link
Member

@jtuttle jtuttle May 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a whole thing (rubocop/ruby-style-guide#176). There's a reason the ruby style guide doesn't push one way or the other. 😂

  1. I prefer having my continuation marker at the end of the line. The indentation of subsequent lines is enough of a signal to show that we're dealing with indentation.
  2. Sure, but this doesn't happen often and isn't that big a deal when you need to fix it.

@h-artzi h-artzi force-pushed the 2173-authenticators branch 2 times, most recently from d5e5485 to 394e076 Compare May 26, 2021 21:52
@h-artzi h-artzi requested a review from jtuttle May 26, 2021 21:52
CHANGELOG.md Outdated
Comment on lines 9 to 11
- Enabled authenticators can now be configured via a configuration file, the
CONJUR_AUTHENTICATORS environment variable, or the authenticator allowlist
API.
[cyberark/conjur##2173](https://github.com/cyberark/conjur/issues/#2173)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h-artzi i'm probably missing something but i couldn't find in the code how authenticators can still be enabled by the CONJUR_AUTHENTICATORS environment variable. can you point me to it?

also - i am not sure that the authenticator allowlist API is GA so not sure that it should be in the changelog. @alexkalish wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The anyway_config gem takes care of the usage of the environment variable CONJUR_AUTHENTICATORS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with Oren. Please drop the reference to the API from the message. It's not a GA feature. Otherwise, LGTM!

@h-artzi h-artzi requested a review from micahlee May 27, 2021 13:18
@h-artzi h-artzi force-pushed the 2173-authenticators branch 2 times, most recently from 7e58c23 to 2a0b719 Compare May 27, 2021 13:52
# Enabling via environment overrides enabling via CLI
env_enabled_authenticators(env) || db_enabled_authenticators
authenticators =
Rails.application.config.conjur_config.authenticators.uniq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving the uniq call to the authenticators method in the config? (We may need to create a new method that extends the parent class behaviour). To me, it feels out of place here to "fix-up" config.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to strip empty/nil items from the array.

env_enabled_authenticators(env) || db_enabled_authenticators
authenticators =
Rails.application.config.conjur_config.authenticators.uniq
authenticators.empty? ? db_enabled_authenticators : authenticators
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rarely feel like the ternary operator reads well. What do you think about something like:

def enabled_authenticators
  Rails.application.config.conjur_config.authenticators.select { |e| !e.to_s.empty? } ||
  db_enabled_authenticators
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer the ternary in this case. Having a negated select that calls several methods on the elements it's selecting hurts my head more than the ternary does. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that we are hopefully going to tear out db_enabled_authenticators soon-ish so whatever we choose here is going to be a temporary inconvenience.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think the ternary is clear here, but here's a non-ternary alternative which does the same thing:

authenticators = 
  Rails.application.config.conjur_config.authenticators.uniq
return authenticators if authenticators

db_enabled_authenticators

@@ -63,5 +71,13 @@ def trusted_proxies_valid?
rescue
false
end

def authenticators_valid?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about checking for a valid authenticator (authn-k8s, authn-ldap, authn-oidc, etc.) rather than just the form of a possible authenticator?

@h-artzi h-artzi force-pushed the 2173-authenticators branch 5 times, most recently from 6ca3e86 to b6e3840 Compare May 27, 2021 17:36
@@ -63,5 +71,18 @@ def trusted_proxies_valid?
rescue
false
end

def authenticators_valid?
# TODO: Ideally we would check against the enabled authenticators
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO found

@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]
### Added
- Enabled authenticators can now be configured via a configuration file, or the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lists should be surrounded by blank lines

# in the DB. However, we need to figure out how to use code from the
# application without introducing warnings.
authenticators_regex =
%r{^(authn|authn-(k8s|oidc|iam|ldap|gcp|azure|config)(/.+)?)$}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove config from here now, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch!

@codeclimate
Copy link

codeclimate bot commented May 27, 2021

Code Climate has analyzed commit 1a00282 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Bug Risk 1
Style 1

The test coverage on the diff in this pull request is 94.1% (50% is the threshold).

This pull request will bring the total coverage in the repository to 89.2%.

View more on Code Climate.

@h-artzi h-artzi merged commit a48be72 into master May 27, 2021
@h-artzi h-artzi deleted the 2173-authenticators branch May 27, 2021 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Enabled authenticators can be configured using configuration file
7 participants