diff --git a/Gemfile b/Gemfile index f1a96a2..924931a 100644 --- a/Gemfile +++ b/Gemfile @@ -17,6 +17,7 @@ group :development, :test do gem "webmock" gem "codecov", require: false, group: "test" gem "simplecov" + gem "dotenv" end # application runtime dependencies @@ -31,3 +32,4 @@ gem "yaml-safe_load_stream", git: "https://github.com/wr0ngway/yaml-safe_load_stream.git", branch: "ruby_3" gem "async" +gem "faraday-cookie_jar" diff --git a/Gemfile.lock b/Gemfile.lock index 05562f0..ccc3f25 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,7 @@ GEM docile (1.4.0) domain_name (0.5.20190701) unf (>= 0.0.5, < 1.0.0) + dotenv (2.7.6) e2mmap (0.1.0) faraday (1.5.1) faraday-em_http (~> 1.0) @@ -58,6 +59,9 @@ GEM faraday-patron (~> 1.0) multipart-post (>= 1.2, < 3) ruby2_keywords (>= 0.0.4) + faraday-cookie_jar (0.0.7) + faraday (>= 0.8.0) + http-cookie (~> 1.0.0) faraday-em_http (1.0.0) faraday-em_synchrony (1.0.0) faraday-excon (1.1.0) @@ -222,6 +226,8 @@ DEPENDENCIES cloudtruth-client! codecov debase + dotenv + faraday-cookie_jar gem_logger kubeclient liquid diff --git a/lib/kubetruth/ctapi.rb b/lib/kubetruth/ctapi.rb index 3ea3716..9798ecc 100644 --- a/lib/kubetruth/ctapi.rb +++ b/lib/kubetruth/ctapi.rb @@ -1,6 +1,7 @@ require 'uri' require "cloudtruth-client" require_relative 'parameter' +require 'faraday-cookie_jar' module Kubetruth class CtApi @@ -40,7 +41,7 @@ def initialize(environment: "default", tag: nil) @environments_mutex = Mutex.new @projects_mutex = Mutex.new @templates_mutex = Mutex.new - + raise ArgumentError.new("CtApi has not been configured") if @@api_key.nil? || @@api_url.nil? @api_key = @@api_key @api_url = @@api_url @@ -58,6 +59,7 @@ def initialize(environment: "default", tag: nil) config.api_key = {'ApiKeyAuth' => @api_key} config.api_key_prefix = {'ApiKeyAuth' => "Api-Key"} config.logger = logger + config.use(:cookie_jar) # config.debugging = logger.debug? @client = CloudtruthClient::ApiClient.new(config) @client.user_agent = "kubetruth/#{Kubetruth::VERSION}" diff --git a/spec/fixtures/vcr/Kubetruth_CtApi/_cookies/uses_session_cookie.yml b/spec/fixtures/vcr/Kubetruth_CtApi/_cookies/uses_session_cookie.yml new file mode 100644 index 0000000..1278c6b --- /dev/null +++ b/spec/fixtures/vcr/Kubetruth_CtApi/_cookies/uses_session_cookie.yml @@ -0,0 +1,130 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.staging.cloudtruth.io/api/v1/environments/ + body: + encoding: US-ASCII + string: '' + headers: + Content-Type: + - application/json + User-Agent: + - kubetruth/1.2.3 + Accept: + - application/json + Authorization: + - Api-Key + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 24 Oct 2023 14:10:31 GMT + Content-Type: + - application/json + Content-Length: + - '2852' + Connection: + - keep-alive + Set-Cookie: + - AWSALBTG=wN+LmUTnRrSEShDOQJldq22JOPAA/pFCk2GvSZjFIT3+Iw9Xd6I4E3Ptroan7SnEq++bdu8wl8Kz7zY6eWPtKiyuX2634iP9KkOtvYWluuodDkWwU2nrP4q5+QguhBaHb6M2yoFqqrtyiHWOHDB4uxxz17v11JOPSFv3DXZWrk9a; + Expires=Tue, 31 Oct 2023 14:10:30 GMT; Path=/ + - AWSALBTGCORS=wN+LmUTnRrSEShDOQJldq22JOPAA/pFCk2GvSZjFIT3+Iw9Xd6I4E3Ptroan7SnEq++bdu8wl8Kz7zY6eWPtKiyuX2634iP9KkOtvYWluuodDkWwU2nrP4q5+QguhBaHb6M2yoFqqrtyiHWOHDB4uxxz17v11JOPSFv3DXZWrk9a; + Expires=Tue, 31 Oct 2023 14:10:30 GMT; Path=/; SameSite=None; Secure + - csrftoken=lINvyvAZoSG1dfZeWPXuVEWVm8QBpJAQCCuKJOYryrYTGhbyxGV1PsvB75v4lMTI; + expires=Tue, 22 Oct 2024 14:10:31 GMT; Max-Age=31449600; Path=/; SameSite=Lax + - sessionid=qacsyisja6fh91czvskro7rv6x3bezvq; expires=Tue, 07 Nov 2023 14:10:31 + GMT; Max-Age=1209600; Path=/; Secure + Server: + - gunicorn + Vary: + - Accept, Cookie, Origin + Allow: + - GET, POST, HEAD, OPTIONS + X-Frame-Options: + - DENY + X-Content-Type-Options: + - nosniff + Referrer-Policy: + - same-origin + Cross-Origin-Opener-Policy: + - same-origin + body: + encoding: UTF-8 + string: '{"count":5,"next":null,"previous":null,"results":[{"url":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","id":"21c25551-8ef7-4510-88a2-a021282d0932","ledger_id":"42f9a65b-ae2e-4528-8ab1-861f003c1f28","name":"default","description":"Default + environment, base for all environments.","parent":null,"children":["https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","https://api.staging.cloudtruth.io/api/v1/environments/d21e7d9c-f551-4be9-8473-b47564788e6f/","https://api.staging.cloudtruth.io/api/v1/environments/89665d29-7b3c-4276-8c8e-3c64e057fd77/"],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.634610Z","modified_at":"2022-03-09T22:27:16.103027Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","id":"84b011d4-6bf8-42c5-ad61-0029d7c09d94","ledger_id":"8397ca2c-10ea-4572-983c-e3b064760256","name":"development","description":"Development + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":["https://api.staging.cloudtruth.io/api/v1/environments/0e48bc6c-80f9-4cfe-ba61-8c76f6af805c/"],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.643813Z","modified_at":"2022-03-09T22:27:20.973257Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/0e48bc6c-80f9-4cfe-ba61-8c76f6af805c/","id":"0e48bc6c-80f9-4cfe-ba61-8c76f6af805c","ledger_id":"df8a6cc3-25ee-444d-b15d-117b235997f3","name":"dev-matt","description":"","parent":"https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","children":[],"access_controlled":false,"role":null,"created_at":"2023-05-05T15:26:07.459733Z","modified_at":"2023-05-05T15:26:07.459733Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/d21e7d9c-f551-4be9-8473-b47564788e6f/","id":"d21e7d9c-f551-4be9-8473-b47564788e6f","ledger_id":"f687b3c8-7d83-4c84-bbce-8a778fff1d1d","name":"production","description":"Production + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":[],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.661538Z","modified_at":"2023-05-12T15:09:59.558343Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/89665d29-7b3c-4276-8c8e-3c64e057fd77/","id":"89665d29-7b3c-4276-8c8e-3c64e057fd77","ledger_id":"9d93cd23-4fcd-4b38-9a32-1e79ff73b838","name":"staging","description":"Staging + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":[],"access_controlled":true,"role":null,"created_at":"2021-07-20T18:38:22.652821Z","modified_at":"2022-03-09T22:27:17.778754Z"}]}' + recorded_at: Tue, 24 Oct 2023 14:10:31 GMT +- request: + method: get + uri: https://api.staging.cloudtruth.io/api/v1/environments/ + body: + encoding: US-ASCII + string: '' + headers: + Content-Type: + - application/json + User-Agent: + - kubetruth/1.2.3 + Accept: + - application/json + Authorization: + - Api-Key + Cookie: + - AWSALBTG=wN+LmUTnRrSEShDOQJldq22JOPAA/pFCk2GvSZjFIT3+Iw9Xd6I4E3Ptroan7SnEq++bdu8wl8Kz7zY6eWPtKiyuX2634iP9KkOtvYWluuodDkWwU2nrP4q5+QguhBaHb6M2yoFqqrtyiHWOHDB4uxxz17v11JOPSFv3DXZWrk9a; + AWSALBTGCORS=wN+LmUTnRrSEShDOQJldq22JOPAA/pFCk2GvSZjFIT3+Iw9Xd6I4E3Ptroan7SnEq++bdu8wl8Kz7zY6eWPtKiyuX2634iP9KkOtvYWluuodDkWwU2nrP4q5+QguhBaHb6M2yoFqqrtyiHWOHDB4uxxz17v11JOPSFv3DXZWrk9a; + csrftoken=lINvyvAZoSG1dfZeWPXuVEWVm8QBpJAQCCuKJOYryrYTGhbyxGV1PsvB75v4lMTI; + sessionid=qacsyisja6fh91czvskro7rv6x3bezvq + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 24 Oct 2023 14:10:32 GMT + Content-Type: + - application/json + Content-Length: + - '2852' + Connection: + - keep-alive + Set-Cookie: + - AWSALBTG=beFOWob2Zq+0nmMh1GXqHKiM3avc8NIB6owDD72ElmC23PiLw+FwmpmyUDSMeEsRwpxyhdT1GiYuRhtURWKZ/81tOccdJp6s7bpB212zL63s9PSGasaCOQIy+mAQAzqMAw0aHvjmmgYUVcKwQJei7iHn1uQ9BQaw53a15IOJrkhl; + Expires=Tue, 31 Oct 2023 14:10:31 GMT; Path=/ + - AWSALBTGCORS=beFOWob2Zq+0nmMh1GXqHKiM3avc8NIB6owDD72ElmC23PiLw+FwmpmyUDSMeEsRwpxyhdT1GiYuRhtURWKZ/81tOccdJp6s7bpB212zL63s9PSGasaCOQIy+mAQAzqMAw0aHvjmmgYUVcKwQJei7iHn1uQ9BQaw53a15IOJrkhl; + Expires=Tue, 31 Oct 2023 14:10:31 GMT; Path=/; SameSite=None; Secure + - csrftoken=7Qxg7rruXgpGVAWqoFajz2EDOU0hkDaBBaPVGOYbgaC6F2ngEWtQf7z10HCx5n8f; + expires=Tue, 22 Oct 2024 14:10:32 GMT; Max-Age=31449600; Path=/; SameSite=Lax + - sessionid=qacsyisja6fh91czvskro7rv6x3bezvq; expires=Tue, 07 Nov 2023 14:10:32 + GMT; Max-Age=1209600; Path=/; Secure + Server: + - gunicorn + Vary: + - Accept, Cookie, Origin + Allow: + - GET, POST, HEAD, OPTIONS + X-Frame-Options: + - DENY + X-Content-Type-Options: + - nosniff + Referrer-Policy: + - same-origin + Cross-Origin-Opener-Policy: + - same-origin + body: + encoding: UTF-8 + string: '{"count":5,"next":null,"previous":null,"results":[{"url":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","id":"21c25551-8ef7-4510-88a2-a021282d0932","ledger_id":"42f9a65b-ae2e-4528-8ab1-861f003c1f28","name":"default","description":"Default + environment, base for all environments.","parent":null,"children":["https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","https://api.staging.cloudtruth.io/api/v1/environments/d21e7d9c-f551-4be9-8473-b47564788e6f/","https://api.staging.cloudtruth.io/api/v1/environments/89665d29-7b3c-4276-8c8e-3c64e057fd77/"],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.634610Z","modified_at":"2022-03-09T22:27:16.103027Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","id":"84b011d4-6bf8-42c5-ad61-0029d7c09d94","ledger_id":"8397ca2c-10ea-4572-983c-e3b064760256","name":"development","description":"Development + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":["https://api.staging.cloudtruth.io/api/v1/environments/0e48bc6c-80f9-4cfe-ba61-8c76f6af805c/"],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.643813Z","modified_at":"2022-03-09T22:27:20.973257Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/0e48bc6c-80f9-4cfe-ba61-8c76f6af805c/","id":"0e48bc6c-80f9-4cfe-ba61-8c76f6af805c","ledger_id":"df8a6cc3-25ee-444d-b15d-117b235997f3","name":"dev-matt","description":"","parent":"https://api.staging.cloudtruth.io/api/v1/environments/84b011d4-6bf8-42c5-ad61-0029d7c09d94/","children":[],"access_controlled":false,"role":null,"created_at":"2023-05-05T15:26:07.459733Z","modified_at":"2023-05-05T15:26:07.459733Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/d21e7d9c-f551-4be9-8473-b47564788e6f/","id":"d21e7d9c-f551-4be9-8473-b47564788e6f","ledger_id":"f687b3c8-7d83-4c84-bbce-8a778fff1d1d","name":"production","description":"Production + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":[],"access_controlled":false,"role":null,"created_at":"2021-07-20T18:38:22.661538Z","modified_at":"2023-05-12T15:09:59.558343Z"},{"url":"https://api.staging.cloudtruth.io/api/v1/environments/89665d29-7b3c-4276-8c8e-3c64e057fd77/","id":"89665d29-7b3c-4276-8c8e-3c64e057fd77","ledger_id":"9d93cd23-4fcd-4b38-9a32-1e79ff73b838","name":"staging","description":"Staging + deployment environment","parent":"https://api.staging.cloudtruth.io/api/v1/environments/21c25551-8ef7-4510-88a2-a021282d0932/","children":[],"access_controlled":true,"role":null,"created_at":"2021-07-20T18:38:22.652821Z","modified_at":"2022-03-09T22:27:17.778754Z"}]}' + recorded_at: Tue, 24 Oct 2023 14:10:32 GMT +recorded_with: VCR 6.0.0 diff --git a/spec/kubetruth/ctapi_spec.rb b/spec/kubetruth/ctapi_spec.rb index be6dadd..b8dcc12 100644 --- a/spec/kubetruth/ctapi_spec.rb +++ b/spec/kubetruth/ctapi_spec.rb @@ -49,6 +49,20 @@ def create_project_fixture end + describe "#cookies" do + + it "uses session cookie" do + data, status_code, headers = ctapi.apis[:environments].environments_list_with_http_info + cookies = headers["set-cookie"] + session_cookie1 = cookies.match(/sessionid=([^;]+)/)[1] + data, status_code, headers = ctapi.apis[:environments].environments_list_with_http_info + cookies = headers["set-cookie"] + session_cookie2 = cookies.match(/sessionid=([^;]+)/)[1] + expect(session_cookie1).to eq(session_cookie2) + end + + end + describe "#environments" do it "gets environments" do @@ -244,7 +258,7 @@ def create_project_fixture expect(ctapi.template("tone", project: @project_name)).to eq("tmpl1 defaultone") sleep 2 - + tag = ctapi.apis[:environments].environments_tags_list(ctapi.environment_id("default"), name: "test_tag").results.first if tag ctapi.apis[:environments].environments_tags_update(ctapi.environment_id("default"), tag.id, CloudtruthClient::TagUpdate.new(name: "test_tag", timestamp: Time.now)) @@ -255,17 +269,17 @@ def create_project_fixture sleep 2 ctapi.apis[:projects].projects_parameters_values_update(one_param_value.id, @one_param.id, @project_id, value_update: CloudtruthClient::ValueUpdate.new(internal_value: "newdefaultone")) - + params = ctapi.parameters(project: @project_name) expect(params.collect(&:value).sort).to eq(["defaulttwo", "newdefaultone"]) expect(ctapi.template("tone", project: @project_name)).to eq("tmpl1 newdefaultone") - + ctapi_tagged = described_class.new(tag: "test_tag") params = ctapi_tagged.parameters(project: @project_name) expect(params.collect(&:value).sort).to eq(["defaultone", "defaulttwo"]) expect(ctapi_tagged.template("tone", project: @project_name)).to eq("tmpl1 defaultone") end - + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 25e0df9..9bcd63d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require "bundler/setup" +require 'dotenv/load' ENV['CLOUDTRUTH_API_KEY'] ||= 'fake_api_key' require "kubetruth"