-
Notifications
You must be signed in to change notification settings - Fork 147
Policy Factory (Alpha Release) #2855
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require './app/domain/responses' | ||
|
|
||
| class PolicyFactoriesController < RestController | ||
| include AuthorizeResource | ||
|
|
||
| before_action :current_user | ||
|
|
||
| def create | ||
| response = DB::Repository::PolicyFactoryRepository.new.find( | ||
| role: current_user, | ||
| **relevant_params(%i[account kind version id]) | ||
| ).bind do |factory| | ||
| Factories::CreateFromPolicyFactory.new.call( | ||
| account: params[:account], | ||
| factory_template: factory, | ||
| request_body: request.body.read, | ||
| authorization: request.headers["Authorization"] | ||
| ) | ||
| end | ||
|
|
||
| render_response(response) do | ||
| render(json: response.result) | ||
| end | ||
| end | ||
|
|
||
| def show | ||
| allowed_params = %i[account kind version id] | ||
| response = DB::Repository::PolicyFactoryRepository.new.find( | ||
| role: current_user, | ||
| **relevant_params(allowed_params) | ||
| ) | ||
|
|
||
| render_response(response) do | ||
| presenter = Presenter::PolicyFactories::Show.new(factory: response.result) | ||
| render(json: presenter.present) | ||
| end | ||
| end | ||
|
|
||
| def index | ||
| response = DB::Repository::PolicyFactoryRepository.new.find_all( | ||
| role: current_user, | ||
| account: params[:account] | ||
| ) | ||
| render_response(response) do | ||
| presenter = Presenter::PolicyFactories::Index.new(factories: response.result) | ||
| render(json: presenter.present) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def render_response(response, &block) | ||
| if response.success? | ||
| block.call | ||
| else | ||
| presenter = Presenter::PolicyFactories::Error.new(response: response) | ||
| render( | ||
| json: presenter.present, | ||
| status: response.status | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def relevant_params(allowed_params) | ||
| params.permit(*allowed_params).slice(*allowed_params).to_h.symbolize_keys | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| require 'base64' | ||
| require 'json' | ||
|
|
||
| require './app/domain/responses' | ||
|
|
||
| module DB | ||
| module Repository | ||
| module DataObjects | ||
| PolicyFactory = Struct.new( | ||
| :name, | ||
| :classification, | ||
| :version, | ||
| :policy, | ||
| :policy_branch, | ||
| :schema, | ||
| :description, | ||
| keyword_init: true | ||
| ) | ||
| end | ||
|
|
||
| class PolicyFactoryRepository | ||
| def initialize( | ||
| data_object: DataObjects::PolicyFactory, | ||
| resource: ::Resource, | ||
| logger: Rails.logger | ||
| ) | ||
| @resource = resource | ||
| @data_object = data_object | ||
| @logger = logger | ||
| @success = ::SuccessResponse | ||
| @failure = ::FailureResponse | ||
| end | ||
|
|
||
| def find_all(account:, role:) | ||
| factories = @resource.visible_to(role).where( | ||
| Sequel.like( | ||
| :resource_id, | ||
| "#{account}:variable:conjur/factories/%" | ||
| ) | ||
| ).all | ||
| .select { |factory| role.allowed_to?(:execute, factory) } | ||
| .group_by do |item| | ||
| # form is: 'conjur/factories/core/v1/groups' | ||
| _, _, classification, _, factory = item.resource_id.split('/') | ||
| [classification, factory].join('/') | ||
| end | ||
| .map do |_, versions| | ||
| versions.max { |a, b| factory_version(a.id) <=> factory_version(b.id) } | ||
| end | ||
| .map do |factory| | ||
| response = secret_to_data_object(factory) | ||
| response.result if response.success? | ||
| end | ||
| .compact | ||
|
|
||
| if factories.empty? | ||
| return @failure.new( | ||
| 'Role does not have permission to use Factories', | ||
| status: :forbidden | ||
| ) | ||
| end | ||
|
|
||
| @success.new(factories) | ||
| end | ||
|
|
||
| def find(kind:, id:, account:, role:, version: nil) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
| factory = if version.present? | ||
| @resource["#{account}:variable:conjur/factories/#{kind}/#{version}/#{id}"] | ||
| else | ||
| @resource.where( | ||
| Sequel.like( | ||
| :resource_id, | ||
| "#{account}:variable:conjur/factories/#{kind}/%" | ||
| ) | ||
| ).all | ||
| .select { |i| i.resource_id.split('/').last == id } | ||
| .max { |a, b| factory_version(a.id) <=> factory_version(b.id) } | ||
| end | ||
|
|
||
| resource_id = "#{kind}/#{version || 'v1'}/#{id}" | ||
|
|
||
| if factory.blank? | ||
| @failure.new( | ||
| { resource: resource_id, message: 'Requested Policy Factory does not exist' }, | ||
| status: :not_found | ||
| ) | ||
| elsif !role.allowed_to?(:execute, factory) | ||
| @failure.new( | ||
| { resource: resource_id, message: 'Requested Policy Factory is not available' }, | ||
| status: :forbidden | ||
| ) | ||
| else | ||
| secret_to_data_object(factory) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def factory_version(factory_id) | ||
| version_match = factory_id.match(%r{/v(\d+)/[\w-]+}) | ||
| return 0 if version_match.nil? | ||
|
|
||
| version_match[1].to_i | ||
| end | ||
|
|
||
| def secret_to_data_object(variable) | ||
| _, _, classification, version, id = variable.resource_id.split('/') | ||
| factory = variable.secret&.value | ||
| if factory | ||
| decoded_factory = JSON.parse(Base64.decode64(factory)) | ||
| @success.new( | ||
| @data_object.new( | ||
| policy: Base64.decode64(decoded_factory['policy']), | ||
| policy_branch: decoded_factory['policy_branch'], | ||
| schema: decoded_factory['schema'], | ||
| version: version, | ||
| name: id, | ||
| classification: classification, | ||
| description: decoded_factory['schema']&.dig('description').to_s | ||
| ) | ||
| ) | ||
| else | ||
| @failure.new( | ||
| { resource: "#{classification}/#{version}/#{id}", message: 'Requested Policy Factory is not available' }, | ||
| status: :bad_request | ||
| ) | ||
|
Comment on lines
+123
to
+126
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this failure case truly a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is rough because it's dependent on prior actions (as you've pointed out). Micah and I have been talking about phase two of these factories, which makes them first class citizens at the policy level (work here). Much of the pre-population issues will go away with this work. As this is only an alpha level feature, how do you feel about addressing this work as part of the next phase?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good to me 👍 |
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method
find_allhas 26 lines of code (exceeds 25 allowed). Consider refactoring.