Class: Strata::CLI::Credentials

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/strata/cli/credentials.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#adapterObject (readonly)

Returns the value of attribute adapter.



17
18
19
# File 'lib/strata/cli/credentials.rb', line 17

def adapter
  @adapter
end

#credentialsObject (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

.fetch(ds_key) ⇒ Object

Retrieve existing credentials for the given ds_key or empty hash.



13
14
15
# File 'lib/strata/cli/credentials.rb', line 13

def self.fetch(ds_key)
  CLI.config[ds_key] || {}
end

Instance Method Details

#collectObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/strata/cli/credentials.rb', line 64

def collected?
  !@credentials.keys.empty?
end

#required?Boolean

Returns:

  • (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.message}"
end