Skip to content

Commit

Permalink
feat: Add credentials API
Browse files Browse the repository at this point in the history
  • Loading branch information
mromulus committed Nov 25, 2024
1 parent ba4516d commit cf3d4ba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/controllers/api/v3/credential_sets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module API
module V3
class CredentialSetsController < APIController
before_action :get_exercise

def index
render json: {
result: scope.map { CredentialSetPresenter.new(_1) }
}
end

private
def scope
authorized_scope(@exercise.credential_sets)
end
end
end
end
59 changes: 59 additions & 0 deletions app/presenters/api/v3/credential_set_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

module API
module V3
class CredentialSetPresenter < Struct.new(:credential_set)
delegate :network, to: :credential_set
delegate :actor, to: :network

def as_json(_opts)
Rails.cache.fetch(['apiv3', credential_set.cache_key_with_version, credential_set.credential_bindings.cache_key_with_version]) do
{
id: credential_set.slug,
name: credential_set.name,
actor: (ActorAPIName.result_for(actor) if network),
numbers:
}
end
end

private
def numbers_configuration
if network
NetworkInstances.result_for(network)
else
['all']
end
end

def numbers
Hash[
numbers_configuration.map { [_1, credentials(_1)] }
]
end

def credentials(number)
credential_set.credentials.map do |cred|
domain_username = if credential_set.network
[
credential_set.network_domain_prefix,
cred.username
].join('\\')
end
{
name: cred.name,
password: cred.password,
username: cred.username,
email: StringSubstituter.result_for(cred.email, { team_nr: number }),
domain_username:,
config_map: JSON.parse(
StringSubstituter.result_for(
JSON.dump(cred.config_map), { team_nr: number }
)
)
}
end
end
end
end
end
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
resources :checks, only: %i[create update destroy]
end
resources :capabilities
if Rails.configuration.x.features.credentials
if Rails.configuration.x.features.dig(:credentials)
resources :credential_sets, path: :credentials do
resources :credentials, only: %i[new create show update destroy] do
collection do
Expand Down Expand Up @@ -68,6 +68,9 @@
resources :services, only: %i[index show]
resources :capabilities, only: %i[index]
resources :tags, only: %i[index]
if Rails.configuration.x.features.dig(:credentials)
resources :credential_sets, path: 'credentials', only: %i[index]
end
resource :inventory, only: %i[show]
resource :graph, only: %i[show]
resources :customization_specs, path: 'hosts', only: %i[index show] do
Expand Down

0 comments on commit cf3d4ba

Please sign in to comment.