Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit cf3c0a7

Browse files
committed
recreate default profile if incoming workspace is different to the existing workspace id in the default profile
1 parent 1488ea1 commit cf3c0a7

1 file changed

Lines changed: 18 additions & 25 deletions

File tree

lib/cipherstash/client/profile.rb

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ def self.load(maybe_name, logger, **opts)
7575
end
7676

7777
def self.create(name, logger, **opts)
78-
puts "###### name #########"
79-
p name
80-
81-
puts "******* opts *********"
82-
p opts
8378
begin
8479
Dir.mkdir(File.expand_path("~/.cipherstash"))
8580
logger.debug("CipherStash::Client::Profile.create") { "Created ~/.cipherstash" }
@@ -95,44 +90,32 @@ def self.create(name, logger, **opts)
9590
logger.debug("CipherStash::Client::Profile.create") { "Created ~/.cipherstash/#{name}" }
9691
rescue Errno::EEXIST
9792
if name == "default"
98-
# Does the provided workspace id match the default profile workspace id.
9993
incoming_workspace_id = opts[:workspace]
100-
puts "@@@ workspace id @@@@@"
101-
puts incoming_workspace_id
94+
95+
10296
profile_config = File.read(File.expand_path("~/.cipherstash/#{name}/profile-config.json"))
103-
10497
parsed_profile_config = JSON.parse(profile_config)
10598

10699
default_profile_workspace_id = parsed_profile_config["service"]["workspace"]
107100

108-
puts "######### default profile workspace id======="
109-
puts default_profile_workspace_id
110-
111101
if incoming_workspace_id == default_profile_workspace_id
112102
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
113103
else
114-
puts "updating default profile"
115-
logger.debug("CipherStash::Client::Profile.create") { "~/.cipherstash/#{name} exists with with Workspace ID: #{default_profile_workspace_id}. Updating default profile to use Workspace ID: #{incoming_workspace_id}." }
104+
logger.debug("CipherStash::Client::Profile.create") { "~/.cipherstash/#{name} created with Workspace ID: #{default_profile_workspace_id}. Updating #{name} profile to use Workspace ID: #{incoming_workspace_id}." }
116105
end
117106
else
118107
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
119108
end
120-
# The default profile name coming through from ActiveStash will be "default" if an env var is not provided for CS_PROFILE_NAME
121-
# A user could provide a workspace id at login that is different to the workspace id in the default profile
122-
# Which will mean, it raises an error here because a default profile already exists.
123-
# The default profile doesn't get updated.
124-
# There isn't a way to update the default profile to use the provided workspace id as the default profile.
125-
126-
# If name is default
127-
# Does the provided workspace id align with the workspace id in the default profile.
128-
# If so, provide error saying profile already exists
129-
# If not, update default profile to use provided workspace id.
130109

131110
rescue => ex
132111
raise Error::CreateProfileFailure, "Could not create profile directory ~/.cipherstash/#{name}: #{ex.message} (#{ex.class})"
133112
end
134113

135114
begin
115+
if name == "default" && File.exists?(File.expand_path("~/.cipherstash/#{name}/auth-token.json"))
116+
File.delete(File.expand_path("~/.cipherstash/#{name}/auth-token.json"))
117+
end
118+
136119
File.write(File.expand_path("~/.cipherstash/#{name}/profile-config.json"), default_profile.to_json)
137120
logger.debug("CipherStash::Client::Profile.create") { "Wrote ~/.cipherstash/#{name}/profile-config.json" }
138121
rescue => ex
@@ -411,7 +394,11 @@ def identity_provider_config
411394
# @see #with_kms_credentials because it has a more in-depth explanation of what's going on and why.
412395
#
413396
def with_access_token(&blk)
397+
puts "block======="
398+
p blk
414399
@access_token_creds_provider ||= access_token_provider(**symbolize_keys(identity_provider_config))
400+
p identity_provider_config
401+
p @access_token_creds_provider
415402

416403
if blk.nil?
417404
@access_token_creds_provider.fresh_credentials
@@ -555,6 +542,7 @@ def access_token_provider(kind:, **opts)
555542
when "Auth0-AccessToken"
556543
access_token_static_credentials(**opts)
557544
when "Auth0-DeviceCode"
545+
puts "hitting auth0 device code-----------"
558546
access_token_device_code_credentials(**opts)
559547
when "Console-AccessKey"
560548
access_token_console_access_key_credentials(**opts)
@@ -575,7 +563,12 @@ def access_token_static_credentials(accessToken:)
575563
#
576564
# If the token can't be read for any reason, just return a null token, because you're supposed to refresh the token if it's out-of-date anyway.
577565
def cached_token
578-
JSON.parse(File.read(file_path("auth-token.json")))
566+
puts "self -----------"
567+
p self
568+
test = JSON.parse(File.read(file_path("auth-token.json")))
569+
puts "auth token json #########"
570+
p test
571+
test
579572
rescue
580573
{ "accessToken": "", "refreshToken": "", expiry: 0 }
581574
end

0 commit comments

Comments
 (0)