Class: HostedChef::ApiClient
- Inherits:
-
Object
- Object
- HostedChef::ApiClient
- Extended by:
- Forwardable
- Defined in:
- lib/hosted-chef/api_client.rb
Overview
Talks to Hosted Chef over HTTP. Right now, uses screen scraping to fetch CSRF tokens from HTML pages, but I’m optimistic that we’ll at least send these as JSON in the future.
Instance Method Summary collapse
-
#initialize(options) ⇒ ApiClient
constructor
A new instance of ApiClient.
-
#knife_config ⇒ Object
Fetches the knife.rb for the given user/organization pair from manage.
-
#login_authenticity_token ⇒ Object
Rails csrf token for the login page.
-
#login_cookies ⇒ Object
Memoized cookies for authN with manage and community sites.
-
#user_key ⇒ Object
Fetches the user’s RSA API key from community.opscode.com.
-
#user_key_authenticity_token ⇒ Object
Rails csrf token for the user profile page on community.opscode.com.
-
#validator_key ⇒ Object
Fetches the organization’s validator client’s key from manage.
-
#validator_key_authenticity_token ⇒ Object
Rails csrf token for the organization list page on manage.opscode.com.
Constructor Details
#initialize(options) ⇒ ApiClient
Returns a new instance of ApiClient.
12 13 14 |
# File 'lib/hosted-chef/api_client.rb', line 12 def initialize() @options = end |
Instance Method Details
#knife_config ⇒ Object
Fetches the knife.rb for the given user/organization pair from manage.
61 62 63 |
# File 'lib/hosted-chef/api_client.rb', line 61 def knife_config RestClient.get("https://manage.opscode.com/organizations/#{orgname}/_knife_config", :cookies => ) end |
#login_authenticity_token ⇒ Object
Rails csrf token for the login page.
17 18 19 20 21 22 |
# File 'lib/hosted-chef/api_client.rb', line 17 def login_authenticity_token @auth_token ||= begin login_page = RestClient.get('https://manage.opscode.com') extract_csrf_token_from(login_page) end end |
#login_cookies ⇒ Object
Memoized cookies for authN with manage and community sites.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/hosted-chef/api_client.rb', line 43 def @login_cookies ||= begin = {:name => username, :password => password, :authenticity_token => login_authenticity_token, :multipart => true} RestClient.post("https://manage.opscode.com/login_exec", ) do |response, req, result, &block| if response.headers[:location] == "https://manage.opscode.com/login" # bad passwd raise InvalidPassword else response. end end end end |
#user_key ⇒ Object
Fetches the user’s RSA API key from community.opscode.com.
NB: Updates state on the server such that any existing key becomes invalid.
69 70 71 72 73 74 75 |
# File 'lib/hosted-chef/api_client.rb', line 69 def user_key = { :authenticity_token => user_key_authenticity_token, :multipart => true } RestClient.post("https://community.opscode.com/users/#{username}/user_key", , {:cookies => }) end |
#user_key_authenticity_token ⇒ Object
Rails csrf token for the user profile page on community.opscode.com
25 26 27 28 29 30 31 |
# File 'lib/hosted-chef/api_client.rb', line 25 def user_key_authenticity_token @user_key_authenticity_token ||= begin new_key_page = RestClient.get("https://community.opscode.com/users/#{username}/user_key/new", :cookies => ) @login_cookies = new_key_page. extract_csrf_token_from(new_key_page) end end |
#validator_key ⇒ Object
Fetches the organization’s validator client’s key from manage.
NB: This updates state on the server such that any existing key for this client is no longer valid.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hosted-chef/api_client.rb', line 81 def validator_key #https://manage.opscode.com/organizations/prodbench2/_regenerate_key = { #:multipart => true, :authenticity_token => validator_key_authenticity_token, :_method => "put" } headers = { :Referer => "https://manage.opscode.com/organizations", :cookies => } uri = "https://manage.opscode.com/organizations/#{orgname}/_regenerate_key" RestClient.post(uri, ,headers) end |
#validator_key_authenticity_token ⇒ Object
Rails csrf token for the organization list page on manage.opscode.com
34 35 36 37 38 39 40 |
# File 'lib/hosted-chef/api_client.rb', line 34 def validator_key_authenticity_token @validator_key_authenticity_token ||= begin org_list_page = RestClient.get("https://manage.opscode.com/organizations", :cookies => ) @login_cookies = org_list_page. extract_csrf_token_from(org_list_page) end end |