| 1 | package Net::Kubernetes::Role::SecretBuilder; | ||||||||||||||
| 2 | # ABSTRACT: Role to allow creation of resources from either objects or files. | ||||||||||||||
| 3 | 4 | $Net::Kubernetes::Role::SecretBuilder::VERSION = '0.19'; | |||||||||||||
| 4 | 4 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use MooseX::Aliases; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | require Net::Kubernetes::Resource::Secret; | |||||||||||||
| 7 | 4 | require Net::Kubernetes::Exception; | |||||||||||||
| 8 | 4 | ▶ | use File::Slurp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use syntax 'try'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | sub build_secret { | ||||||||||||||
| 14 | 0 | ▶ | my($self, $name, $data) = @_; | ||||||||||||
|
Subroutine
build_secret: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 15 | 0 | return $self->create($self->_assemble_secret($name, $data)); | |||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | sub _assemble_secret { | ||||||||||||||
| 19 | 0 | ▶ | my($self, $name, $data) = @_; | ||||||||||||
|
Subroutine
_assemble_secret: not called CC=6 SCAR=37.4
| |||||||||||||||
| 20 | 0 | my($secret) = { | |||||||||||||
| 21 | kind => 'Secret', | ||||||||||||||
| 22 | apiVersion => $self->api_version, | ||||||||||||||
| 23 | metadata=>{ | ||||||||||||||
| 24 | name => $name, | ||||||||||||||
| 25 | }, | ||||||||||||||
| 26 | type => 'Opaque', | ||||||||||||||
| 27 | id => $name, | ||||||||||||||
| 28 | data => {}, | ||||||||||||||
| 29 | namespace=>'default', | ||||||||||||||
| 30 | }; | ||||||||||||||
| 31 | 0 | foreach my $key (keys %$data){ | |||||||||||||
| 32 | 0 | ▶ | if (ref $data->{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 0 | my $value; | |||||||||||||
| 34 | # Handle serializing data | ||||||||||||||
| 35 | 0 | ▶ | if (uc($data->{$key}{type}) eq 'JSON') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 36 | 0 | $value = $self->json->encode($data->{$key}{value}); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | elsif (uc($data->{$key}{type}) eq 'YAML') { | ||||||||||||||
| 39 | 0 | $value = YAML::XS::Dump($data->{$key}{value}); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | else{ | ||||||||||||||
| 42 | 0 | $value = $data->{$key}{value}; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | 0 | $secret->{data}{$key} = encode_base64($value, ""); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | # if passed a string, it should be a filename | ||||||||||||||
| 48 | 0 | ▶ | if (! -f $data->{$key}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | Net::Kubernetes::Exception->throw(message => "Failed to build secret: $data->{$key} - Not Such file."); | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | 0 | $secret->{data}{$key} = encode_base64(read_file($data->{$key}), ""); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | } | ||||||||||||||
| 55 | 0 | return $secret; | |||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | return 42; | |||||||||||||
| 59 | |||||||||||||||
| 60 | __END__ | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =pod | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =encoding UTF-8 | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head1 NAME | ||||||||||||||
| 67 | |||||||||||||||
| 68 | Net::Kubernetes::Role::SecretBuilder - Role to allow creation of resources from either objects or files. | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =head1 VERSION | ||||||||||||||
| 71 | |||||||||||||||
| 72 | version 0.19 | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =head1 METHODS | ||||||||||||||
| 75 | |||||||||||||||
| 76 | =head2 build_secret($name, $data) | ||||||||||||||
| 77 | |||||||||||||||
| 78 | Builds a Kubernetes secret object with $name. $data is a hash reference whose keys will be keys int the created secret. | ||||||||||||||
| 79 | |||||||||||||||
| 80 | The value for each key should be either a filename (which will be slurped into | ||||||||||||||
| 81 | the secret), or a hashref with the keys "type" and "value". | ||||||||||||||
| 82 | |||||||||||||||
| 83 | Valid types are "JSON", "YAML" or "String" (anything other that "JSON" or | ||||||||||||||
| 84 | "YAML") will be assumed to be of type "String". If either "JSON" or "YAML" the | ||||||||||||||
| 85 | "value" will be serialized out before placing in the secret. | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Note that the keys must be valid DNS subdomains (underscore is not allowed) and must be lowercase. | ||||||||||||||
| 88 | |||||||||||||||
| 89 | my ($new_secret) = $kube->build_secret('donttell', { | ||||||||||||||
| 90 | ssh-public-key => '/home/dave/.ssh/id_rsa.pub', | ||||||||||||||
| 91 | super-secret-data => { | ||||||||||||||
| 92 | type => 'JSON', | ||||||||||||||
| 93 | value => { username => 'Dave', password => 'Imnottelling' }, | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | }); | ||||||||||||||
| 96 | |||||||||||||||
| 97 | =head1 AUTHOR | ||||||||||||||
| 98 | |||||||||||||||
| 99 | Dave Mueller <dave@perljedi.com> | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 102 | |||||||||||||||
| 103 | This software is Copyright (c) 2015 by Dave Mueller. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | This is free software, licensed under: | ||||||||||||||
| 106 | |||||||||||||||
| 107 | The MIT (X11) License | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 SEE ALSO | ||||||||||||||
| 110 | |||||||||||||||
| 111 | Please see those modules/websites for more information related to this module. | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =over 4 | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =item * | ||||||||||||||
| 116 | |||||||||||||||
| 117 | L<Net::Kubernetes|Net::Kubernetes> | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =back | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =cut | ||||||||||||||