Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webui][api] interface to access a project's public key and ssl certi…
…ficate rename PublicKeyInfoController to PublicKey & use models for handling the backend response use find_by! instead of having unecssary extra line revert the find_by! commit WIP: add webui project publickey controller change class to module change to multi-line class namespace syntax remove webui public key controller model: Project::PublicKey => PublicKey Revert "remove webui public key controller" This reverts commit 53d2f3e. webui projects controller lets you download the public key enable eager_load in development make the public key controller publicly accessible controller spec which uses stub_request add 404 response and link to download public key add webui controller spec use faker to generate the "gpg key" for rspec tests memoize project.public_key return the ssl cert as well in the xml response add ssl certificate controller move the source api url from the specs to the model so its only defined in one place
- Loading branch information
Showing
14 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Project | ||
class PublicKeyController < ApplicationController | ||
before_action :extract_user_public | ||
skip_before_action :extract_user | ||
skip_before_action :require_login | ||
|
||
def show | ||
project = Project.find_by_name!(params[:project_name]) | ||
|
||
if project.public_key.present? | ||
render :show, locals: { public_key: project.public_key } | ||
else | ||
render nothing: true, status: :not_found | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/api/app/controllers/webui/project/public_key_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Webui | ||
module Project | ||
class PublicKeyController < WebuiController | ||
def show | ||
project = ::Project.find_by_name!(params[:project_name]) | ||
|
||
if project.public_key.present? | ||
send_data( | ||
project.public_key.content, | ||
disposition: 'attachment', | ||
filename: "#{project.title}_key.pub" | ||
) | ||
else | ||
render nothing: true, status: :not_found | ||
end | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
src/api/app/controllers/webui/project/ssl_certificate_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Webui | ||
module Project | ||
class SslCertificateController < WebuiController | ||
def show | ||
project = ::Project.find_by_name!(params[:project_name]) | ||
|
||
if project.public_key.present? && project.public_key.ssl_certificate.present? | ||
send_data( | ||
project.public_key.ssl_certificate, | ||
disposition: 'attachment', | ||
filename: "#{project.title}_ssl.cert" | ||
) | ||
else | ||
render nothing: true, status: :not_found | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class PublicKey | ||
include ActiveModel::Model | ||
|
||
attr_accessor :content, :algorithm, :ssl_certificate | ||
|
||
def self.find_by_project_name(project_name) | ||
response = Suse::Backend.get(backend_url(project_name)).body | ||
parsed_response = Xmlhash.parse(response) | ||
|
||
if parsed_response['pubkey'].present? | ||
public_key_params = { | ||
content: parsed_response['pubkey']['_content'], | ||
algorithm: parsed_response['pubkey']['algo'] | ||
} | ||
|
||
if parsed_response['sslcert'].present? | ||
public_key_params[:ssl_certificate] = parsed_response['sslcert'] | ||
end | ||
|
||
new(public_key_params) | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.backend_url(project_name) | ||
"/source/#{project_name}/_keyinfo?withsslcert=1" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
xml.public_key algorithm: public_key.algorithm do | ||
xml.content public_key.content | ||
xml.ssl_certificate public_key.ssl_certificate | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/api/spec/controllers/project/public_key_controller_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'rails_helper' | ||
require 'webmock/rspec' | ||
|
||
RSpec.describe Project::PublicKeyController, type: :controller, vcr: true do | ||
describe 'GET #show' do | ||
let(:project) do | ||
create( | ||
:project, | ||
name: "test_project", | ||
title: "Test Project" | ||
) | ||
end | ||
|
||
let(:backend_url) { CONFIG['source_url'] + PublicKey.send(:backend_url, project.name) } | ||
|
||
before do | ||
stub_request(:get, backend_url).and_return(body: keyinfo_response) | ||
|
||
get :show, params: { format: :xml, project_name: project.name } | ||
end | ||
|
||
context 'with a project that has a public key and an ssl certificate' do | ||
let(:gpg_public_key) { Faker::Lorem.characters(1024) } | ||
let(:ssl_certificate) { Faker::Lorem.characters(1024) } | ||
let(:keyinfo_response) do | ||
%Q{<keyinfo project="Test"><pubkey algo="rsa">#{gpg_public_key}</pubkey><sslcert>#{ssl_certificate}</sslcert></keyinfo>} | ||
end | ||
|
||
it { is_expected.to respond_with(:success) } | ||
it { is_expected.to render_template(:show) } | ||
end | ||
|
||
context 'with a project that has a public key and no ssl certificate' do | ||
let(:gpg_public_key) { Faker::Lorem.characters(1024) } | ||
let(:keyinfo_response) do | ||
%Q{<keyinfo project="Test"><pubkey algo="rsa">#{gpg_public_key}</pubkey></keyinfo>} | ||
end | ||
|
||
it { is_expected.to respond_with(:success) } | ||
it { is_expected.to render_template(:show) } | ||
end | ||
|
||
context 'with a project that has no public key' do | ||
let(:keyinfo_response) { '<keyinfo />' } | ||
|
||
it { expect(response.status).to eq(404) } | ||
end | ||
end | ||
end |
38 changes: 38 additions & 0 deletions
38
src/api/spec/controllers/webui/project/public_key_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'rails_helper' | ||
require 'webmock/rspec' | ||
|
||
RSpec.describe Webui::Project::PublicKeyController, type: :controller, vcr: true do | ||
describe 'GET #show' do | ||
let(:project) do | ||
create( | ||
:project, | ||
name: "test_project", | ||
title: "Test Project" | ||
) | ||
end | ||
|
||
let(:backend_url) { CONFIG['source_url'] + PublicKey.send(:backend_url, project.name) } | ||
|
||
before do | ||
stub_request(:get, backend_url).and_return(body: keyinfo_response) | ||
|
||
get :show, params: { project_name: project.name } | ||
end | ||
|
||
context 'with a project that has a public key' do | ||
let(:gpg_public_key) { Faker::Lorem.characters(1024) } | ||
let(:keyinfo_response) do | ||
%Q{<keyinfo project="Test"><pubkey algo="rsa">#{gpg_public_key}</pubkey></keyinfo>} | ||
end | ||
|
||
it { expect(response.header['Content-Disposition']).to include('attachment') } | ||
it { expect(response.body.strip).to eq(gpg_public_key) } | ||
end | ||
|
||
context 'with a project that has no public key' do | ||
let(:keyinfo_response) { '<keyinfo />' } | ||
|
||
it { expect(response.status).to eq(404) } | ||
end | ||
end | ||
end |
42 changes: 42 additions & 0 deletions
42
src/api/spec/controllers/webui/project/ssl_certificate_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'rails_helper' | ||
require 'webmock/rspec' | ||
|
||
RSpec.describe Webui::Project::SslCertificateController, type: :controller, vcr: true do | ||
describe 'GET #show' do | ||
let(:project) do | ||
create( | ||
:project, | ||
name: "test_project", | ||
title: "Test Project" | ||
) | ||
end | ||
|
||
let(:backend_url) { CONFIG['source_url'] + PublicKey.send(:backend_url, project.name) } | ||
|
||
before do | ||
stub_request(:get, backend_url).and_return(body: keyinfo_response) | ||
|
||
get :show, params: { project_name: project.name } | ||
end | ||
|
||
context 'with a project that has an ssl certificate' do | ||
let(:gpg_public_key) { Faker::Lorem.characters(1024) } | ||
let(:ssl_certificate) { Faker::Lorem.characters(1024) } | ||
let(:keyinfo_response) do | ||
%Q{<keyinfo project="Test"><pubkey algo="rsa">#{gpg_public_key}</pubkey><sslcert>#{ssl_certificate}</sslcert></keyinfo>} | ||
end | ||
|
||
it { expect(response.header['Content-Disposition']).to include('attachment') } | ||
it { expect(response.body.strip).to eq(ssl_certificate) } | ||
end | ||
|
||
context 'with a project that has no ssl certificate' do | ||
let(:gpg_public_key) { Faker::Lorem.characters(1024) } | ||
let(:keyinfo_response) do | ||
%Q{<keyinfo project="Test"><pubkey algo="rsa">#{gpg_public_key}</pubkey></keyinfo>} | ||
end | ||
|
||
it { expect(response.status).to eq(404) } | ||
end | ||
end | ||
end |