PublicKey.to_s()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
# frozen_string_literal: true
2
3
require 'digest'
4
5
require_relative '../mixin/entity'
6
7
module AMA
8
  module Chef
9
    module User
10
      module Model
11
        class PublicKey
12
          include Mixin::Entity
13
14
          attribute :owner, Symbol
15
          attribute :id, Symbol
16
          attribute :type, Symbol, default: :'ssh-rsa'
17
          attribute :content, String, sensitive: true, nullable: true
18
          attribute :digest, String
19
          attribute :validate, TrueClass, FalseClass, default: true
20
21
          denormalizer_block do |input, type, context, &block|
22
            input = { content: input } if input.is_a?(String)
23
            raise "Hash expected, got #{input.class}" unless input.is_a?(Hash)
24
            { id: -1, owner: -3 }.each do |key, index|
25
              if [key, key.to_s].none? { |v| input.key?(v) }
26
                segment = context.path.segments[index]
27
                input[key] = segment.name unless segment.nil?
28
              end
29
            end
30
            block.call(input, type, context)
31
          end
32
33
          normalizer_block do |entity, type, context, &block|
34
            normalized = block.call(entity, type, context)
35
            if context.include_sensitive_attributes
36
              normalized[:content] = entity.content
37
            end
38
            normalized
39
          end
40
41
          def full_id
42
            raise 'Missing owner' unless @owner
43
            raise 'Missing id' unless @id
44
            "#{@owner}:#{@id}"
45
          end
46
47
          def content=(content)
48
            @digest = content.nil? ? nil : Digest::MD5.hexdigest(content)
49
            @content = content
50
          end
51
52
          def to_s
53
            "PublicKey #{owner}:#{id}"
54
          end
55
        end
56
      end
57
    end
58
  end
59
end
60