summaryrefslogtreecommitdiff
path: root/barbicanclient/barbican_cli/v1/containers.py
blob: ccb541b4c2fccc98d6420b4bf2ee97bdd0c44c8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Command-line interface sub-commands related to containers.
"""
from cliff import command
from cliff import lister
from cliff import show

from barbicanclient.v1.containers import CertificateContainer
from barbicanclient.v1.containers import Container
from barbicanclient.v1.containers import RSAContainer


class DeleteContainer(command.Command):
    """Delete a container by providing its href."""

    def get_parser(self, prog_name):
        parser = super(DeleteContainer, self).get_parser(prog_name)
        parser.add_argument('URI', help='The URI reference for the container')
        return parser

    def take_action(self, args):
        self.app.client_manager.key_manager.containers.delete(args.URI)


class GetContainer(show.ShowOne):
    """Retrieve a container by providing its URI."""

    def get_parser(self, prog_name):
        parser = super(GetContainer, self).get_parser(prog_name)
        parser.add_argument('URI', help='The URI reference for the container.')
        return parser

    def take_action(self, args):
        entity = self.app.client_manager.key_manager.containers.get(
            args.URI)
        return entity._get_formatted_entity()


class ListContainer(lister.Lister):
    """List containers."""

    def get_parser(self, prog_name):
        parser = super(ListContainer, self).get_parser(prog_name)
        parser.add_argument('--limit', '-l', default=10,
                            help='specify the limit to the number of items '
                                 'to list per page (default: %(default)s; '
                                 'maximum: 100)',
                            type=int)
        parser.add_argument('--offset', '-o', default=0,
                            help='specify the page offset '
                                 '(default: %(default)s)',
                            type=int)
        parser.add_argument('--name', '-n', default=None,
                            help='specify the container name '
                                 '(default: %(default)s)')
        parser.add_argument('--type', '-t', default=None,
                            help='specify the type filter for the list '
                                 '(default: %(default)s).')
        return parser

    def take_action(self, args):
        obj_list = self.app.client_manager.key_manager.containers.list(
            args.limit, args.offset, args.name, args.type)
        return Container._list_objects(obj_list)


class CreateContainer(show.ShowOne):
    """Store a container in Barbican."""

    def get_parser(self, prog_name):
        parser = super(CreateContainer, self).get_parser(prog_name)
        parser.add_argument('--name', '-n',
                            help='a human-friendly name.')
        parser.add_argument('--type', default='generic',
                            help='type of container to create (default: '
                                 '%(default)s).')
        parser.add_argument('--secret', '-s', action='append',
                            help='one secret to store in a container '
                                 '(can be set multiple times). Example: '
                                 '--secret "private_key='
                                 'https://url.test/v1/secrets/1-2-3-4"')
        return parser

    def take_action(self, args):
        client = self.app.client_manager.key_manager
        container_type = client.containers._container_map.get(args.type)
        if not container_type:
            raise ValueError('Invalid container type specified.')
        secret_refs = CreateContainer._parse_secrets(args.secret)
        if container_type is RSAContainer:
            public_key_ref = secret_refs.get('public_key')
            private_key_ref = secret_refs.get('private_key')
            private_key_pass_ref = secret_refs.get('private_key_passphrase')
            entity = RSAContainer(
                api=client.containers._api,
                name=args.name,
                public_key_ref=public_key_ref,
                private_key_ref=private_key_ref,
                private_key_passphrase_ref=private_key_pass_ref,
            )
        elif container_type is CertificateContainer:
            certificate_ref = secret_refs.get('certificate')
            intermediates_ref = secret_refs.get('intermediates')
            private_key_ref = secret_refs.get('private_key')
            private_key_pass_ref = secret_refs.get('private_key_passphrase')
            entity = CertificateContainer(
                api=client.containers._api,
                name=args.name,
                certificate_ref=certificate_ref,
                intermediates_ref=intermediates_ref,
                private_key_ref=private_key_ref,
                private_key_passphrase_ref=private_key_pass_ref,
            )
        else:
            entity = container_type(api=client.containers._api,
                                    name=args.name, secret_refs=secret_refs)
        entity.store()
        return entity._get_formatted_entity()

    @staticmethod
    def _parse_secrets(secrets):
        if not secrets:
            raise ValueError("Must supply at least one secret.")
        return dict(
            (s.split('=')[0], s.split('=')[1])
            for s in secrets if s.count('=') == 1
        )