Sha256: 62fddce93c7be1657d71b670b7a731c2955bf3129b13c84d57950213e061d14f

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

require 'sshkey'
require 'cabal/util'
require 'sekrat'
require 'sekrat/crypter/aes'

module Cabal
  module API
    class ClusterService
      attr_accessor :public_keys, :private_keys

      def initialize(public_warehouse: STORAGE[:public], private_warehouse: STORAGE[:private])
        @public_keys = Sekrat.manager(warehouse: public_warehouse)

        @private_keys = Sekrat.manager(warehouse: private_warehouse, crypter: Sekrat::Crypter::Aes)
      end

      def create(name)
        sshkey = SSHKey.generate(
          type: 'RSA',
          bits: 2048,
          comment: "#{name}-cabal"
        )

        write_public_key(name, sshkey) && write_private_key(name, sshkey)
      end

      def names
        public_keys.ids
      end

      def public_key(name)
        begin
          public_keys.get(name, name)
        rescue
          nil
        end
      end

      def private_key(name)
        begin
          private_keys.get(name, public_key(name))
        rescue
          nil
        end
      end

      private
      def write_public_key(name, key)
        begin
          public_keys.put(name, name, key.ssh_public_key)
          true
        rescue
          return false
        end
      end

      def write_private_key(name, key)
        begin
          private_keys.put(
            name,
            key.ssh_public_key,
            key.private_key
          )
          true
        rescue
          false
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cabal-api-0.2.4 lib/cabal/api/cluster_service.rb
cabal-api-0.2.3 lib/cabal/api/cluster_service.rb