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

Commit 1488ea1

Browse files
committed
add logic to check incoming workspace id and existing workspace id in default profile
1 parent ce39f84 commit 1488ea1

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

lib/cipherstash/client/profile.rb

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ 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
7883
begin
7984
Dir.mkdir(File.expand_path("~/.cipherstash"))
8085
logger.debug("CipherStash::Client::Profile.create") { "Created ~/.cipherstash" }
@@ -89,7 +94,40 @@ def self.create(name, logger, **opts)
8994
Dir.mkdir(File.expand_path("~/.cipherstash/#{name}"))
9095
logger.debug("CipherStash::Client::Profile.create") { "Created ~/.cipherstash/#{name}" }
9196
rescue Errno::EEXIST
92-
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
97+
if name == "default"
98+
# Does the provided workspace id match the default profile workspace id.
99+
incoming_workspace_id = opts[:workspace]
100+
puts "@@@ workspace id @@@@@"
101+
puts incoming_workspace_id
102+
profile_config = File.read(File.expand_path("~/.cipherstash/#{name}/profile-config.json"))
103+
104+
parsed_profile_config = JSON.parse(profile_config)
105+
106+
default_profile_workspace_id = parsed_profile_config["service"]["workspace"]
107+
108+
puts "######### default profile workspace id======="
109+
puts default_profile_workspace_id
110+
111+
if incoming_workspace_id == default_profile_workspace_id
112+
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
113+
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}." }
116+
end
117+
else
118+
raise Error::CreateProfileFailure, "Could not create profile #{name.inspect}: already exists"
119+
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.
130+
93131
rescue => ex
94132
raise Error::CreateProfileFailure, "Could not create profile directory ~/.cipherstash/#{name}: #{ex.message} (#{ex.class})"
95133
end

0 commit comments

Comments
 (0)