Module: GraphQL::RakeTask::Validate

Defined in:
lib/graphql/rake_task/validate.rb

Overview

Helpers for graphql:pro:validate. Credentials are never passed to a subprocess, so they can't leak into the process list or logs.

Constant Summary collapse

CREDENTIALS_PATTERN =
/\A[a-z0-9]{11}:[a-z0-9]{11}\z/
DIGEST_PATTERN =
/\A\h{128}\z/

Class Method Summary collapse

Class Method Details

.credentials ⇒ Object

Returns user:pass for gems.graphql.pro, or nil if it isn't configured.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/rake_task/validate.rb', line 32

def credentials
  creds = if defined?(Bundler) && Bundler.respond_to?(:settings)
    Bundler.settings["gems.graphql.pro"]
  end
  if creds.nil?
    # Array form doesn't use a shell, and no secrets are in the arguments
    output = IO.popen(["bundle", "config", "get", "gems.graphql.pro", "--parseable"], err: File::NULL, &:read)
    creds = output[/[a-z0-9]{11}:[a-z0-9]{11}/]
  end
  creds = creds&.strip
  creds && creds.match?(CREDENTIALS_PATTERN) ? creds : nil
rescue SystemCallError
  nil
end

.fetch_gem_digest(version, creds) ⇒ Object

Returns the SHA512 hex digest of the downloaded gem, or nil if the download failed.



48
49
50
51
52
53
54
55
# File 'lib/graphql/rake_task/validate.rb', line 48

def fetch_gem_digest(version, creds)
  user, pass = creds.split(":", 2)
  uri = URI.parse("https://gems.graphql.pro/gems/graphql-pro-#{version}.gem")
  req = Net::HTTP::Get.new(uri)
  req.basic_auth(user, pass)
  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
  res.is_a?(Net::HTTPSuccess) ? Digest::SHA512.hexdigest(res.body.to_s) : nil
end

.fetch_published_digest(uri) ⇒ Object

Returns the published digest at uri, or nil if it couldn't be fetched.



58
59
60
61
62
63
64
# File 'lib/graphql/rake_task/validate.rb', line 58

def fetch_published_digest(uri)
  res = Net::HTTP.get_response(uri)
  return nil unless res.is_a?(Net::HTTPSuccess)
  # Remove final newline from .txt file
  digest = res.body.to_s.strip
  digest.match?(DIGEST_PATTERN) ? digest : nil
end

.normalize_version(version) ⇒ Object

Returns a normalized version string, or nil if version isn't a valid gem version.



21
22
23
24
25
26
27
28
29
# File 'lib/graphql/rake_task/validate.rb', line 21

def normalize_version(version)
  # `Gem::Version.correct?` accepts `nil` and `""`, so check those first
  if !version.is_a?(String) || version.strip.empty? || !Gem::Version.correct?(version)
    nil
  else
    # Strips surrounding whitespace, leaving only `[0-9a-zA-Z.]`
    Gem::Version.new(version).to_s
  end
end