diff --git a/app/controllers/huawei/code_arts_checks_controller.rb b/app/controllers/huawei/code_arts_checks_controller.rb new file mode 100644 index 000000000..d1d5225f2 --- /dev/null +++ b/app/controllers/huawei/code_arts_checks_controller.rb @@ -0,0 +1,47 @@ +class Huawei::CodeArtsChecksController < ApplicationController + before_action :load_repository + before_action :signer + def index + task_id = "xxxxxx" # 创建任务返回的 task_id + url = "https://codearts-check.#{region}.myhuaweicloud.com/v2/#{project_id}/task/#{task_id}" + + headers = @signer.sign_request("GET", url) + + response = RestClient.get(url, headers) + + puts JSON.pretty_generate(JSON.parse(response.body)) + + end + def create + branch = params[:branch] || "master" + project_id = @project.id + region = "cn-north-4" + url = "https://codearts-check.#{region}.myhuaweicloud.com/v2/#{project_id}/task" + + + body = { + "name":@project.name, + "project_id":project_id, + "git_url": @project.repository.url, + "branch": branch + }.to_json + + headers = @signer.sign_request("POST", url, {}, body) + + response = RestClient.post(url, body, headers) + + puts JSON.pretty_generate(JSON.parse(response.body)) + + + end + + + + private + + def signer + ak = ENV['HUAWEI_CLOUD_AK'] + sk = ENV['HUAWEI_CLOUD_SK'] + @signer = Huawei::Signer.new(ak, sk) + end +end diff --git a/app/services/huawei/signer.rb b/app/services/huawei/signer.rb new file mode 100644 index 000000000..3229a5b88 --- /dev/null +++ b/app/services/huawei/signer.rb @@ -0,0 +1,53 @@ +require 'openssl' +require 'base64' +require 'json' +require 'uri' +require 'time' + +class Huawei::Signer + def initialize(ak, sk) + @ak = ak + @sk = sk + end + + def sign_request(method, url, headers = {}, body = "") + uri = URI.parse(url) + host = uri.host + path = uri.path.empty? ? "/" : uri.path + query = uri.query ? "?" + uri.query : "" + + # 时间戳 + x_sdk_date = Time.now.utc.strftime("%Y%m%dT%H%M%SZ") + + # Step 1: CanonicalRequest + canonical_headers = "host:#{host}\n" \ + "x-sdk-date:#{x_sdk_date}\n" + + signed_headers = "host;x-sdk-date" + + hashed_payload = Digest::SHA256.hexdigest(body) + + canonical_request = "#{method}\n#{path}\n#{query}\n#{canonical_headers}\n#{signed_headers}\n#{hashed_payload}" + + # Step 2: StringToSign + hashed_canonical = Digest::SHA256.hexdigest(canonical_request) + string_to_sign = "SDK-HMAC-SHA256\n#{x_sdk_date}\n#{hashed_canonical}" + + # Step 3: 计算签名 + signature = OpenSSL::HMAC.hexdigest("SHA256", @sk, string_to_sign) + + # Step 4: 生成 Authorization + authorization = "SDK-HMAC-SHA256 Access=#{@ak}, SignedHeaders=#{signed_headers}, Signature=#{signature}" + + # 合并 headers + signed_headers_hash = { + "Authorization" => authorization, + "X-Sdk-Date" => x_sdk_date, + "Host" => host, + "Content-Type" => "application/json" + } + + headers.merge!(signed_headers_hash) + headers + end +end diff --git a/config/routes.rb b/config/routes.rb index 086ff5261..3cdb4a19f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,6 +42,11 @@ Rails.application.routes.draw do scope '/api' do resources :topics, only: [:index] + + namespace :huawei do + resources :code_arts_checks,only: [:index, :create] + end + namespace :ci do resources :languages, only: [:index, :show] do collection do