Skip to content

Commit

Permalink
Support for bearer token and api key
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin <[email protected]>
  • Loading branch information
Sachin committed Oct 30, 2024
1 parent 86243c3 commit ae30dfd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/chef-cli/policyfile/artifactory_cookbook_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,25 @@ def desc
end

def artifactory_api_key
chef_config&.artifactory_api_key || ENV["ARTIFACTORY_API_KEY"]
chef_config&.artifactory_api_key || (ENV["ARTIFACTORY_API_KEY"] unless ENV["ARTIFACTORY_API_KEY"].to_s.strip.empty?)
end

def artifactory_identity_key
chef_config&.artifactory_identity_key || (ENV["ARTIFACTORY_IDENTITY_KEY"] unless ENV["ARTIFACTORY_IDENTITY_KEY"].to_s.strip.empty?)
end

private

def auth_headers
if artifactory_identity_key
{ "Authorization" => "Bearer #{artifactory_identity_key}" }
else
{ "X-Jfrog-Art-API" => artifactory_api_key }
end
end

def http_connection_for(base_url)
headers = { "X-Jfrog-Art-API" => artifactory_api_key }
@http_connections[base_url] ||= Chef::HTTP::Simple.new(base_url, headers:)
@http_connections[base_url] ||= Chef::HTTP::Simple.new(base_url, headers: auth_headers)
end

def full_community_graph
Expand Down
26 changes: 26 additions & 0 deletions spec/unit/policyfile/artifactory_cookbook_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,30 @@
end
end
end

describe "#artifactory_identity_key" do
before do
ENV["ARTIFACTORY_IDENTITY_KEY"] = "test"
end

context "when config is not present" do
let(:config) { nil }
it "should get artifactory key from the env" do
expect(subject.artifactory_identity_key).to eq("test")
end
end

context "when config is present" do
let(:config) { double("Chef::Config") }
it "should get artifactory key from config when key is present" do
expect(config).to receive(:artifactory_identity_key).and_return "test1"
expect(subject.artifactory_identity_key).to eq("test1")
end
it "should get artifactory key from env when config is present but has a nil key" do
expect(config).to receive(:artifactory_identity_key).and_return nil
expect(subject.artifactory_identity_key).to eq("test")
end
end
end

end

0 comments on commit ae30dfd

Please sign in to comment.