Module: Conjur::Authn

Defined in:
lib/conjur/authn.rb

Defined Under Namespace

Classes: NoCredentialsError

Class Method Summary collapse

Class Method Details

.ask_for_credentials(options = {}) ⇒ Object

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/conjur/authn.rb', line 102

def ask_for_credentials(options = {})
  raise NoCredentialsError if options[:noask]

  # also use stderr here, because we might be prompting for a password as part
  # of a command like user:create that we'd want to send to a file.
  require 'highline'
  require 'conjur/api'

  hl = HighLine.new $stdin, $stderr
  
  user = options[:username] || hl.ask("Enter your username to log into Conjur: ")
  pass = options[:password] || hl.ask("Please enter #{options[:username] ? [ options[:username] , "'s" ].join : "your"} password (it will not be echoed): "){ |q| q.echo = false }
    
  api_key = if cas_server = options[:"cas-server"]
    Conjur::API.(user, pass, cas_server)
  else
    Conjur::API.(user, pass)
  end
  @credentials = [user, api_key]
end

.authenticate(options = {}) ⇒ Object



44
45
46
47
# File 'lib/conjur/authn.rb', line 44

def authenticate(options = {})
  require 'conjur/api'
  Conjur::API.authenticate(*get_credentials(options))
end

.connect(cls = nil, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/conjur/authn.rb', line 123

def connect(cls = nil, options = {})
  if cls.nil?
    require 'conjur/api'
    require 'conjur/base'
    cls = Conjur::API
  end
  if token = token_from_environment
    cls.new_from_token token
  else
    cls.new_from_key(*get_credentials(options))
  end
end

.delete_credentialsObject



49
50
51
52
# File 'lib/conjur/authn.rb', line 49

def delete_credentials
  netrc.delete host
  netrc.save
end

.env_credentialsObject



77
78
79
80
81
82
83
# File 'lib/conjur/authn.rb', line 77

def env_credentials
  if ( = ENV['CONJUR_AUTHN_LOGIN']) && (api_key = ENV['CONJUR_AUTHN_API_KEY'])
    [ , api_key ]
  else
    nil
  end
end

.fetch_credentials(options = {}) ⇒ Object Also known as: save_credentials



89
90
91
92
# File 'lib/conjur/authn.rb', line 89

def fetch_credentials(options = {})
  ask_for_credentials(options)
  write_credentials
end

.get_credentials(options = {}) ⇒ Object



73
74
75
# File 'lib/conjur/authn.rb', line 73

def get_credentials(options = {})
  @credentials ||= (env_credentials || read_credentials || fetch_credentials(options))
end

.hostObject



54
55
56
# File 'lib/conjur/authn.rb', line 54

def host
  Conjur::Authn::API.host
end

.login(options = {}) ⇒ Object



39
40
41
42
# File 'lib/conjur/authn.rb', line 39

def (options = {})
  delete_credentials
  get_credentials(options)
end

.netrcObject



58
59
60
# File 'lib/conjur/authn.rb', line 58

def netrc
  @netrc ||= read_netrc
end

.read_credentialsObject



85
86
87
# File 'lib/conjur/authn.rb', line 85

def read_credentials
  netrc[host]
end

.read_netrcObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/conjur/authn.rb', line 62

def read_netrc
  args = []
  if path = Conjur::Config[:netrc_path]
    args.unshift(path)
  else
    path = Netrc.default_path
  end
  fail_if_world_readable path
  Netrc.read(*args)
end

.write_credentialsObject



96
97
98
99
100
# File 'lib/conjur/authn.rb', line 96

def write_credentials
  netrc[host] = @credentials
  netrc.save
  @credentials
end