collins_auth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/lib/collins_auth.rb +70 -0
  2. data/license.txt +13 -0
  3. data/readme.md +42 -0
  4. metadata +77 -0
@@ -0,0 +1,70 @@
1
+ require 'collins_client'
2
+ require 'highline/import'
3
+ require 'yaml'
4
+ require 'socket'
5
+
6
+ module Collins
7
+ module Authenticator
8
+ def self.setup_client(options = {prompt: false})
9
+ if options[:config_file] and not File.readable? options[:config_file]
10
+ raise 'unable to read invalid config file: ' + options[:config_file]
11
+ end
12
+
13
+ Collins::Client.new load_config(options)
14
+ end
15
+
16
+ def self.load_config(options = {})
17
+ conf = (read_config(options[:config_file]) || {}).merge(options) unless options[:prompt] == :only
18
+
19
+ # check if we have all that we expect
20
+ if [:username, :password, :host].all? {|key| conf.keys.include? key}
21
+ return conf
22
+ end
23
+
24
+ # Something is missing. Can we prompt for it?
25
+ if options[:prompt]
26
+ conf.merge!(prompt_creds(conf))
27
+ else
28
+ raise "could not load any valid configuration."
29
+ end
30
+
31
+ conf
32
+ end
33
+
34
+ private
35
+ def self.file2conf(file)
36
+ if file and File.readable? file
37
+ # YAML config has keys as strings but we want symbols
38
+ YAML.load_file(file).reduce({}) do |hash, (key, value)|
39
+ hash[begin key.to_sym rescue key end] = value
40
+ hash
41
+ end
42
+ end
43
+ end
44
+
45
+ def self.prompt_creds(conf={})
46
+ puts 'collins information:'
47
+ conf[:username] = ask('username: ') {|user| user.default = conf[:username] || ENV['USER']}
48
+ conf[:password] ||= ask('password: ') {|password| password.echo = false}
49
+ conf[:host] ||= ask('host: ') {|host| host.default = ['https://collins', get_domain].compact.join('.')}
50
+ conf
51
+ end
52
+
53
+ def self.read_config(config_file = nil)
54
+ config_file ||= [ENV['COLLINS_CLIENT_CONFIG'], File.join(ENV['HOME'], '.collins.yml'), '/etc/collins.yml', '/var/db/collins.yml'].compact.find do |config_file|
55
+ File.readable? config_file and File.size(config_file) > 0
56
+ end
57
+
58
+ file2conf config_file
59
+ end
60
+
61
+ def self.get_domain
62
+ hostname = Socket.gethostname.downcase.split('.')
63
+
64
+ if hostname.length > 1
65
+ hostname.shift
66
+ hostname.join('.')
67
+ end
68
+ end
69
+ end
70
+ end
data/license.txt ADDED
@@ -0,0 +1,13 @@
1
+ Copyright [2014] [Tumblr, Inc.]
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/readme.md ADDED
@@ -0,0 +1,42 @@
1
+ collins_auth
2
+ ============
3
+
4
+ This is a library to make it easy to obtain an authenticated collins_client object.
5
+ It attempts to load credentials from the following yaml files ENV['COLLINS_CLIENT_CONFIG'], ~/.collins.yml, /etc/collins.yml, /var/db/collins.yml, and supports user input.
6
+
7
+ Installation
8
+ ============
9
+
10
+ $ gem install collins_auth
11
+
12
+ Usage
13
+ =====
14
+
15
+ #!/usr/bin/env ruby
16
+ require 'collins_auth'
17
+
18
+ # setup a client from configuration files (ENV['COLLINS_CLIENT_CONFIG'], ~/.collins.yml, /etc/collins.yml or /var/db/collins.yml)
19
+ client = Collins::Authenticator.setup_client
20
+
21
+ # setup a client by loading an available config like above, but prompting the user for any remaining required parameters
22
+ client = Collins::Authenticator.setup_client prompt: true
23
+
24
+ # setup a client by prompting the user for configuration
25
+ client = Collins::Authenticator.setup_client prompt: :only
26
+
27
+ client.find hostname: /^abc.+/
28
+
29
+ License
30
+ ============
31
+
32
+ Copyright 2014 Tumblr, Inc.
33
+
34
+ Licensed under the Apache License, Version 2.0 (the "License");
35
+ you may not use this file except in compliance with the License.
36
+ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
37
+
38
+ Unless required by applicable law or agreed to in writing, software
39
+ distributed under the License is distributed on an "AS IS" BASIS,
40
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41
+ See the License for the specific language governing permissions and
42
+ limitations under the License.
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collins_auth
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Michael Benedict
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-11-22 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: collins_client
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: highline
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: This is a library to make it easy to obtain an authenticated collins_client object. It attempts to load credentials from the following yaml files ENV['COLLINS_CLIENT_CONFIG'], ~/.collins.yml, /etc/collins.yml, /var/db/collins.yml, and supports user input.
38
+ email: benedict@tumblr.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/collins_auth.rb
47
+ - readme.md
48
+ - license.txt
49
+ homepage: https://github.com/tumblr/collins/tree/master/support/ruby/collins-auth
50
+ licenses:
51
+ - Apache License 2.0
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.10
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Library to aid in getting an authenticated Collins::Client
76
+ test_files: []
77
+