Class: KnifeGithubTokenCreate::GithubTokenCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- KnifeGithubTokenCreate::GithubTokenCreate
- Defined in:
- lib/chef/knife/github_token_create.rb
Instance Method Summary collapse
-
#create_github_token(params) ⇒ Object
Create the OAuth authentication token for the knife-github application.
- #delete_github_token(params) ⇒ Object
-
#get_github_token ⇒ Object
Get the OAuth authentication token from config or command line.
- #run ⇒ Object
-
#send_request(params) ⇒ Object
Post Get the OAuth authentication token from config or command line.
-
#update_knife_config(token) ⇒ Object
Updates the knife configuration with the token information inside ~/.chef/knife.rb.
-
#validate_github_token(username = nil) ⇒ Object
Validate the OAuth authentication token for the knife-github application.
Instance Method Details
#create_github_token(params) ⇒ Object
Create the OAuth authentication token for the knife-github application.
165 166 167 168 169 170 171 |
# File 'lib/chef/knife/github_token_create.rb', line 165 def create_github_token(params) Chef::Log.debug("Creating new application token for user: #{username}.") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" params[:body] = '{"note":"knife-github","scopes":["delete_repo", "user", "public_repo", "repo", "gist"]"}' params[:action] = "POST" send_request(params) end |
#delete_github_token(params) ⇒ Object
173 174 175 176 177 178 |
# File 'lib/chef/knife/github_token_create.rb', line 173 def delete_github_token(params) Chef::Log.debug("Deleting token id: #{params[':id']}") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations/#{params[:id]}" params[:action] = "DELETE" send_request(params) end |
#get_github_token ⇒ Object
Get the OAuth authentication token from config or command line
112 113 114 115 116 117 118 119 |
# File 'lib/chef/knife/github_token_create.rb', line 112 def get_github_token() token = locate_config_value('github_token') if token.nil? || token.empty? return nil else return token end end |
#run ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/knife/github_token_create.rb', line 62 def run extend Chef::Mixin::ShellOut # validate base options from base module. # Display information if debug mode is on. display_debug_info # Get the name_args from the command line username = name_args.first # Get token information token = get_github_token() unless config[:force] # Create github token if needed if token.nil? token = validate_github_token(username) update_knife_config(token) end puts "Finished updating your token. Using key:#{token}" end |
#send_request(params) ⇒ Object
Post Get the OAuth authentication token from config or command line
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/chef/knife/github_token_create.rb', line 188 def send_request(params) url = params[:url] Chef::Log.debug("URL: " + url.to_s) uri = URI.parse(url) http = Net::HTTP.new(uri.host,uri.port) if uri.scheme == "https" http.use_ssl = true if @github_ssl_verify_mode == "verify_none" http.verify_mode = OpenSSL::SSL::VERIFY_NONE else http.verify_mode = OpenSSL::SSL::VERIFY_PEER end end if params[:action] == "GET" req = Net::HTTP::Get.new(uri.path) elsif params[:action] == "POST" req = Net::HTTP::Post.new(uri.path) elsif params[:action] == "DELETE" req = Net::HTTP::Delete.new(uri.path) end if params[:token].nil? req.basic_auth params[:username], params[:password] else req.initheader = ({"Authorization" => "token #{params[:token]}"}) end req.body = params[:body] if params[:body] response = http.request(req) unless response.code =~ /^2../ then puts "Error #{response.code}: #{response.}" puts JSON.pretty_generate(JSON.parse(response.body)) puts "URL: #{url}" exit 1 end return nil if response.body.nil? || response.body.empty? begin json = JSON.parse(response.body) rescue ui.warn "The result on the REST Request is not in json format" ui.warn "Output: " + response.body exit 1 end json end |
#update_knife_config(token) ⇒ Object
Updates the knife configuration with the token information inside ~/.chef/knife.rb
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/chef/knife/github_token_create.rb', line 89 def update_knife_config(token) contents = '' update = false config = File.join(ENV["HOME"], ".chef/knife.rb") File.foreach(config) do |line| if line =~ /^\s*knife\[:github_token\].*/ && !token.nil? Chef::Log.debug("Replacing current token with: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" update = true else contents = contents << line end end unless update Chef::Log.debug("Updating configuration with token: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" end File.open(config, 'w') {|f| f.write(contents) } return true end |
#validate_github_token(username = nil) ⇒ Object
Validate the OAuth authentication token for the knife-github application.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef/knife/github_token_create.rb', line 124 def validate_github_token(username=nil) params = {} username = ENV["USER"] if username.nil? params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" Chef::Log.debug("Validating token information for user: #{username}.") params[:username] = username params[:password] = HighLine.new.ask("Please enter github password for #{username} :") { |q| q.echo = "x" } params[:action] = "GET" token_key = nil result = send_request(params) result.each do |token| if token['app'] && token['app']['name'] == "knife-github (API)" if token['scopes'].include?("delete_repo") Chef::Log.debug("Found and using token: #{token_key}") token_key = token['token'] else Chef::Log.debug("Found token: #{token_key} but wrong scope, deleting token.") params[:id] = token['id'] delete_github_token(params) end end end if token_key.nil? result = create_github_token(params) token_key = result['token'] end return token_key end |