# class Key

### Public Instance Methods

fingerprintable_key() click to toggle source

    # File app/models/key.rb, line 30
    def fingerprintable_key
      return true unless key # Don't test if there is no key.
      # `ssh-keygen -lf /dev/stdin <<< "#{key}"` errors with: redirection unexpected
      file = Tempfile.new('key_file')
      begin
        file.puts key
        file.rewind
        fingerprint_output = %xssh-keygen -lf #{file.path} 2>&1` # Catch stderr.
      ensure
        file.close
        file.unlink # deletes the temp file
      end
      errors.add(:key, "can't be fingerprinted") if fingerprint_output.match("failed")
    end

is_deploy_key() click to toggle source

    # File app/models/key.rb, line 53
    def is_deploy_key
      true if project_id
    end

last_deploy?() click to toggle source

    # File app/models/key.rb, line 66
    def last_deploy?
      Key.where(identifier: identifier).count == 0
    end

projects() click to toggle source

projects that has this key

    # File app/models/key.rb, line 58
    def projects
      if is_deploy_key
        [project]
      else
        user.projects
      end
    end

set_identifier() click to toggle source

    # File app/models/key.rb, line 45
    def set_identifier
      if is_deploy_key
        self.identifier = "deploy_#{Digest::MD5.hexdigest(key)}"
      else
        self.identifier = "#{user.identifier}_#{Time.now.to_i}"
      end
    end

strip_white_space() click to toggle source

    # File app/models/key.rb, line 18
    def strip_white_space
      self.key = self.key.strip unless self.key.blank?
    end

unique_key() click to toggle source

    # File app/models/key.rb, line 22
    def unique_key
      query = Key.where(key: key)
      query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id
      if (query.count > 0)
        errors.add :key, 'already exist.'
      end
    end