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

add roles when using conjurctl policy load #1510

Merged
merged 1 commit into from
May 6, 2020

Conversation

h-artzi
Copy link
Contributor

@h-artzi h-artzi commented Apr 27, 2020

What does this PR do?

When using conjurctl to load a policy, it failed to create role credentials. This PR allows the creation of roles.

What ticket does this PR close?

dap-support/#80

How to test?

Check the ticket and run it locally with the appropriate image. It is expected to successfully run and output the created roles.

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

Note: I am not sure that this should be a supported feature in the future.

Follow-on issues

  • Follow-up issue(s) have been logged (and links included below) to update documentation or related projects, or
  • No follow-up issues are required

@h-artzi h-artzi requested a review from a team April 27, 2020 20:20
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch 3 times, most recently from 11e21a2 to 83e7c29 Compare April 28, 2020 13:22
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch from ccd2313 to d3147ed Compare April 28, 2020 14:04
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch from a32b4ac to 67c40c5 Compare April 28, 2020 15:29
credentials = Credentials[role: role] || Credentials.create(role: role)
memo[role.id] = { id: role.id, api_key: credentials.api_key }
memo
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd split this whole thing up into 2 statements, rather than chaining.

Also, see how the above has the awkward pattern

memo[blah] = stuff.....
memo

each_with_object is made for avoiding just this.

Copy link
Contributor

Choose a reason for hiding this comment

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

So this is exactly what I meant.... buuuuuut

I don't get how it's working because each_with_object reverses the arguments relative to reduce, so that the memo is in the 2nd arg:

https://ruby-doc.org/core-2.7.1/Enumerable.html#method-i-each_with_object

@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch from 67c40c5 to 933f74c Compare May 4, 2020 20:23
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch 3 times, most recently from b491c48 to 238df9a Compare May 5, 2020 01:21
@h-artzi h-artzi requested review from jtuttle and jonahx May 5, 2020 01:23
jtuttle
jtuttle previously requested changes May 5, 2020
Copy link
Contributor

@jtuttle jtuttle left a comment

Choose a reason for hiding this comment

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

Please fix the codeclimate errors. Break that load method up into helpers.

@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch 2 times, most recently from a0f0634 to 8f490bd Compare May 5, 2020 12:48

created_roles = accepted_roles.each_with_object({}) do |role, memo|
credentials = Credentials[role: role] || Credentials.create(role: role)
memo[role.id] = { id: role.id, api_key: credentials.api_key }
Copy link

Choose a reason for hiding this comment

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

RootLoader#create_roles calls 'role.id' 2 times

end
end

def create_roles policy_version
Copy link

Choose a reason for hiding this comment

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

RootLoader#create_roles has approx 8 statements

Copy link
Contributor

Choose a reason for hiding this comment

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

I just noticed this after approving, but per CC you should extract this:

      loader = Loader::Orchestrate.new policy_version
      loader.load
      accepted_roles = loader.new_roles.select do |role|
        %w(user host).member?(role.kind)
      end

into an accepted_roles(policy_version) method.

jonahx
jonahx previously approved these changes May 5, 2020
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch from 8f490bd to ea9caf7 Compare May 5, 2020 14:06
@h-artzi h-artzi requested a review from jonahx May 5, 2020 14:09
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch 2 times, most recently from 1b4de2e to ae2c656 Compare May 5, 2020 14:48
policy_version = policy_version(role: admin, policy: root_policy_resource, filename: filename)

accepted_roles = accepted_roles(policy_version)
create_roles(accepted_roles)
Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid confusion with the method name, we should use something diff for the local var name:

        acc_roles = accepted_roles(policy_version)
        create_roles(acc_roles)

@@ -23,19 +25,45 @@ def load account, filename
end

root_policy_resource = Loader::Types.find_or_create_root_policy(account)
policy_version = policy_version(role: admin, policy: root_policy_resource, filename: filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

split long line:

policy_version = policy_version(
  role: admin,
  policy: root_policy_resource,
  filename: filename  
)

and per other comment, use something different from local var name than the method name, policy_ver or something.

@h-artzi h-artzi changed the title add roles when using conjurctl policy load WIP - add roles when using conjurctl policy load May 5, 2020
jonahx
jonahx previously approved these changes May 5, 2020
Copy link
Contributor

@jonahx jonahx left a comment

Choose a reason for hiding this comment

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

👍

@h-artzi h-artzi requested a review from jtuttle May 5, 2020 20:05
@h-artzi h-artzi changed the title WIP - add roles when using conjurctl policy load add roles when using conjurctl policy load May 5, 2020
policy_version.perform_automatic_deletion =
policy_version.delete_permitted =
policy_version.update_permitted = true
policy_version.save
Copy link
Contributor

Choose a reason for hiding this comment

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

What's happening here? It looks like something went wrong...

Copy link
Contributor

Choose a reason for hiding this comment

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

Just being naughty and seeing if reek would complain. 🤭

Was trying a multiple assignment to get around the statement limit.

Copy link
Contributor

Choose a reason for hiding this comment

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

And I would have gotten away with it too if it wasn't for that darn indentation warning! 🐕

Copy link
Contributor

Choose a reason for hiding this comment

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

hahahahaha

This is a clear case where reek may be ignored. BUT.... the deeper reason it's throwing the warning is because the API for PolicyVersion.new uses the "create and configure" pattern, which is poor and should be avoided. Instead the constructor should take a hash and return an immutable object.

Also, what does the dog emoji mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

[true] * 3

😆 😆

I think it's time to admit defeat and just ignore the CC error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hah yeah I jokingly suggested [true] * 3 to Hadar too. 😂

The dog emoji was a reference to Scooby Doo's "and I would have gotten away with it too if it wasn't for you darn kids!" 😄

@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch 2 times, most recently from 0cd9b69 to 68a3d37 Compare May 6, 2020 13:23
jonahx
jonahx previously approved these changes May 6, 2020
@h-artzi h-artzi force-pushed the conjurctl_load_host_and_user branch from 68a3d37 to 497873f Compare May 6, 2020 13:26
@codeclimate
Copy link

codeclimate bot commented May 6, 2020

Code Climate has analyzed commit 497873f and detected 0 issues on this pull request.

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

This pull request will bring the total coverage in the repository to 85.4% (0.0% change).

View more on Code Climate.

@h-artzi h-artzi merged commit b2a8abb into master May 6, 2020
@h-artzi h-artzi deleted the conjurctl_load_host_and_user branch May 7, 2020 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants