Class: HostedChef::ApiClient

Inherits:
Object
  • Object
show all
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

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)
  @options = options
end

Instance Method Details

#knife_configObject

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_tokenObject

Rails csrf token for the login page.



17
18
19
20
21
22
# File 'lib/hosted-chef/api_client.rb', line 17

def 
  @auth_token ||= begin
     = RestClient.get('https://manage.opscode.com')
    extract_csrf_token_from()
  end
end

#login_cookiesObject

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
    post_options = {:name => username,
                    :password => password,
                    :authenticity_token => ,
                    :multipart => true}

    RestClient.post("https://manage.opscode.com/login_exec", post_options) do |response, req, result, &block|
      if response.headers[:location] == "https://manage.opscode.com/login" # bad passwd
        raise InvalidPassword
      else
        response.cookies
      end
    end
  end
end

#user_keyObject

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
  post_options = {
    :authenticity_token => user_key_authenticity_token,
    :multipart => true
  }
  RestClient.post("https://community.opscode.com/users/#{username}/user_key", post_options, {:cookies => })
end

#user_key_authenticity_tokenObject

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.cookies
    extract_csrf_token_from(new_key_page)
  end
end

#validator_keyObject

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
  post_options = {
    #: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, post_options,headers)
end

#validator_key_authenticity_tokenObject

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.cookies
    extract_csrf_token_from(org_list_page)
  end
end