Class: Cert::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cert/runner.rb

Instance Method Summary collapse

Instance Method Details

#certificate_typeObject

The kind of certificate we’re interested in



78
79
80
81
82
83
84
# File 'lib/cert/runner.rb', line 78

def certificate_type
  cert_type = Spaceship.certificate.production
  cert_type = Spaceship.certificate.development if Cert.config[:development]
  cert_type = Spaceship.certificate.in_house if Spaceship.client.in_house?

  cert_type
end

#certificatesObject

All certificates of this type



73
74
75
# File 'lib/cert/runner.rb', line 73

def certificates
  certificate_type.all
end

#create_certificateObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cert/runner.rb', line 86

def create_certificate
  # Create a new certificate signing request
  csr, pkey = Spaceship.certificate.create_certificate_signing_request

  # Use the signing request to create a new distribution certificate
  begin
    certificate = certificate_type.create!(csr: csr)
  rescue => ex
    if ex.to_s.include?("You already have a current")
      Helper.log.error "Could not create another certificate, reached the maximum number of available certificates.".red
    end

    raise ex
  end

  # Store all that onto the filesystem

  request_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.certSigningRequest"))
  File.write(request_path, csr.to_pem)

  private_key_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.p12"))
  File.write(private_key_path, pkey)

  cert_path = store_certificate(certificate)

  # Import all the things into the Keychain
  KeychainImporter.import_file(private_key_path)
  KeychainImporter.import_file(cert_path)

  # Environment variables for the fastlane action
  ENV["CER_CERTIFICATE_ID"] = certificate.id
  ENV["CER_FILE_PATH"] = cert_path

  Helper.log.info "Successfully generated #{certificate.id} which was imported to the local machine.".green

  return cert_path
end

#find_existing_certObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cert/runner.rb', line 38

def find_existing_cert
  certificates.each do |certificate|
    path = store_certificate(certificate)
    private_key_path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.p12"))

    if FastlaneCore::CertChecker.installed?(path)
      # This certificate is installed on the local machine
      ENV["CER_CERTIFICATE_ID"] = certificate.id
      ENV["CER_FILE_PATH"] = path

      Helper.log.info "Found the certificate #{certificate.id} (#{certificate.name}) which is installed on the local machine. Using this one.".green

      return path
    elsif File.exist?(private_key_path)
      KeychainImporter.import_file(private_key_path)
      KeychainImporter.import_file(path)

      ENV["CER_CERTIFICATE_ID"] = certificate.id
      ENV["CER_FILE_PATH"] = path

      Helper.log.info "Found the cached certificate #{certificate.id} (#{certificate.name}). Using this one.".green

      return path
    else
      Helper.log.info "Certificate #{certificate.id} (#{certificate.name}) can't be found on your local computer"
    end

    File.delete(path) # as apparantly this certificate is pretty useless without a private key
  end

  Helper.log.info "Couldn't find an existing certificate... creating a new one"
  return nil
end

#launchObject



5
6
7
8
9
10
11
# File 'lib/cert/runner.rb', line 5

def launch
  run

  installed = FastlaneCore::CertChecker.installed?(ENV["CER_FILE_PATH"])
  raise "Could not find the newly generated certificate installed" unless installed
  return ENV["CER_FILE_PATH"]
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cert/runner.rb', line 13

def run
  FileUtils.mkdir_p(Cert.config[:output_path])

  FastlaneCore::PrintTable.print_values(config: Cert.config, hide_keys: [], title: "Summary for cert #{Cert::VERSION}")

  Helper.log.info "Starting login with user '#{Cert.config[:username]}'"
  Spaceship.(Cert.config[:username], nil)
  Spaceship.select_team
  Helper.log.info "Successfully logged in"

  should_create = Cert.config[:force]
  unless should_create
    cert_path = find_existing_cert
    should_create = cert_path.nil?
  end

  return unless should_create

  if create_certificate # no certificate here, creating a new one
    return # success
  else
    raise "Something went wrong when trying to create a new certificate..."
  end
end

#store_certificate(certificate) ⇒ Object



124
125
126
127
128
129
# File 'lib/cert/runner.rb', line 124

def store_certificate(certificate)
  path = File.expand_path(File.join(Cert.config[:output_path], "#{certificate.id}.cer"))
  raw_data = certificate.download_raw
  File.write(path, raw_data)
  return path
end