Class: Strata::CLI::Credentials
- Inherits:
-
Object
- Object
- Strata::CLI::Credentials
- Includes:
- Thor::Shell
- Defined in:
- lib/strata/cli/credentials.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
Class Method Summary collapse
-
.fetch(ds_key) ⇒ Object
Retrieve existing credentials for the given ds_key or empty hash.
Instance Method Summary collapse
- #collect ⇒ Object
- #collected? ⇒ Boolean
-
#initialize(adapter) ⇒ Credentials
constructor
A new instance of Credentials.
- #required? ⇒ Boolean
- #write_local(ds_key, context) ⇒ Object
Constructor Details
#initialize(adapter) ⇒ Credentials
Returns a new instance of Credentials.
19 20 21 22 |
# File 'lib/strata/cli/credentials.rb', line 19 def initialize(adapter) @adapter = adapter.downcase.strip @prompt = TTY::Prompt.new end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
17 18 19 |
# File 'lib/strata/cli/credentials.rb', line 17 def adapter @adapter end |
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
17 18 19 |
# File 'lib/strata/cli/credentials.rb', line 17 def credentials @credentials end |
Class Method Details
Instance Method Details
#collect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/strata/cli/credentials.rb', line 28 def collect credentials = {} case adapter when "snowflake" auth_mode = @prompt.select("Authentication mode:", %w[pat kp oauth], default: "pat") credentials["auth_mode"] = auth_mode case auth_mode when "pat" credentials["personal_access_token"] = @prompt.ask("Enter Personal Access Token:") when "kp" credentials["username"] = @prompt.ask("Enter Username:") credentials["private_key"] = @prompt.ask("Enter Private Key Absolute Path:") when "oauth" credentials["oauth_client_id"] = @prompt.ask("OAuth Client ID:") credentials["oauth_client_secret"] = @prompt.ask("OAuth Client Secret:") credentials["oauth_redirect_uri"] = @prompt.ask("OAuth Redirect URI:", default: "https://localhost:3420/callback") oauth_scope = @prompt.ask("OAuth Scope (optional):") credentials["oauth_scope"] = oauth_scope unless oauth_scope.empty? end when "athena" credentials["access_key_id"] = @prompt.ask("AWS Access Key ID:") credentials["secret_access_key"] = @prompt.ask("AWS Secret Access Key:") else if required? unless %w[postgres mysql trino sqlserver].include?(adapter) credentials["username"] = @prompt.ask("Enter Username:") end credentials["password"] = @prompt.mask("Enter Password:") end end @credentials = credentials end |
#collected? ⇒ Boolean
64 65 66 |
# File 'lib/strata/cli/credentials.rb', line 64 def collected? !@credentials.keys.empty? end |
#required? ⇒ Boolean
24 25 26 |
# File 'lib/strata/cli/credentials.rb', line 24 def required? adapter != "duckdb" end |
#write_local(ds_key, context) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/strata/cli/credentials.rb', line 68 def write_local(ds_key, context) context.gsub_file Configuration::STRATA_CONFIG_FILE, Utils.yaml_block_matcher(ds_key), "" creds = "#{ds_key}:" credentials.each do |key, val| creds << "\n #{key}: #{val}" unless val.nil? || val.empty? end context.append_to_file Configuration::STRATA_CONFIG_FILE, creds # Set restrictive permissions (read/write for owner only) strata_file = Configuration::STRATA_CONFIG_FILE File.chmod(0o600, strata_file) if File.exist?(strata_file) rescue Errno::EACCES => e raise Strata::CommandError, "Permission denied setting permissions on #{strata_file}: #{e.}" end |