1# -*- coding: utf-8 -*- #
2# Copyright 2017 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Flags for binauthz command group."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import unicode_literals
20
21from googlecloudsdk.calliope.concepts import concepts
22from googlecloudsdk.command_lib.kms import flags as kms_flags
23from googlecloudsdk.command_lib.util.concepts import concept_parsers
24from googlecloudsdk.command_lib.util.concepts import presentation_specs as presentation_specs_lib
25
26
27def _GetNoteResourceSpec():
28  return concepts.ResourceSpec(
29      'containeranalysis.projects.notes',
30      resource_name='note',
31      projectsId=concepts.ResourceParameterAttributeConfig(
32          name='project',
33          help_text=(
34              'The Container Analysis project for the {resource}.'),
35      ),
36      notesId=concepts.ResourceParameterAttributeConfig(
37          name='note',
38          help_text='The Container Analysis Note ID for the {resource}.',
39      )
40  )
41
42
43def _FormatArgName(base_name, positional):
44  if positional:
45    return base_name.replace('-', '_').upper()
46  else:
47    return '--' + base_name.replace('_', '-').lower()
48
49
50def GetAuthorityNotePresentationSpec(group_help,
51                                     base_name='authority-note',
52                                     required=True,
53                                     positional=True,
54                                     use_global_project_flag=False):
55  """Construct a resource spec for an attestation authority note flag."""
56  flag_overrides = None
57  if not use_global_project_flag:
58    flag_overrides = {
59        'project': _FormatArgName('{}-project'.format(base_name), positional),
60    }
61  return presentation_specs_lib.ResourcePresentationSpec(
62      name=_FormatArgName(base_name, positional),
63      concept_spec=_GetNoteResourceSpec(),
64      group_help=group_help,
65      required=required,
66      flag_name_overrides=flag_overrides,
67  )
68
69
70def _GetAttestorResourceSpec():
71  return concepts.ResourceSpec(
72      'binaryauthorization.projects.attestors',
73      resource_name='attestor',
74      projectsId=concepts.DEFAULT_PROJECT_ATTRIBUTE_CONFIG,
75      attestorsId=concepts.ResourceParameterAttributeConfig(
76          name='name',
77          help_text='The ID of the {resource}.',
78      ))
79
80
81def GetAttestorPresentationSpec(group_help,
82                                base_name='attestor',
83                                required=True,
84                                positional=True,
85                                use_global_project_flag=True):
86  """Construct a resource spec for an attestor flag."""
87  flag_overrides = None
88  if not use_global_project_flag:
89    flag_overrides = {
90        'project': _FormatArgName('{}-project'.format(base_name), positional),
91    }
92  return presentation_specs_lib.ResourcePresentationSpec(
93      name=_FormatArgName(base_name, positional),
94      concept_spec=_GetAttestorResourceSpec(),
95      group_help=group_help,
96      required=required,
97      flag_name_overrides=flag_overrides,
98  )
99
100
101def _GetCryptoKeyVersionResourceSpec():
102  return concepts.ResourceSpec(
103      kms_flags.CRYPTO_KEY_VERSION_COLLECTION,
104      resource_name='CryptoKeyVersion',
105      projectsId=concepts.DEFAULT_PROJECT_ATTRIBUTE_CONFIG,
106      locationsId=concepts.ResourceParameterAttributeConfig(
107          name='location',
108          help_text='The location of the {resource}.',
109      ),
110      keyRingsId=concepts.ResourceParameterAttributeConfig(
111          name='keyring',
112          help_text='The keyring of the {resource}.',
113      ),
114      cryptoKeysId=concepts.ResourceParameterAttributeConfig(
115          name='key',
116          help_text='The key of the {resource}.',
117      ),
118      cryptoKeyVersionsId=concepts.ResourceParameterAttributeConfig(
119          name='version',
120          help_text='The key version of the {resource}.',
121      ),
122  )
123
124
125def GetCryptoKeyVersionPresentationSpec(
126    group_help,
127    base_name='keyversion',
128    required=True,
129    positional=True,
130    use_global_project_flag=True):
131  """Construct a resource spec for a CryptoKeyVersion flag."""
132  flag_overrides = None
133  if not use_global_project_flag:
134    flag_overrides = {
135        'project': _FormatArgName('{}-project'.format(base_name), positional),
136    }
137  return presentation_specs_lib.ResourcePresentationSpec(
138      name=_FormatArgName(base_name, positional),
139      concept_spec=_GetCryptoKeyVersionResourceSpec(),
140      group_help=group_help,
141      required=required,
142      prefixes=not use_global_project_flag,
143      flag_name_overrides=flag_overrides,
144  )
145
146
147def AddConcepts(parser, *presentation_specs):
148  concept_parsers.ConceptParser(presentation_specs).AddToParser(parser)
149
150
151def AddArtifactUrlFlag(parser, required=True):
152  parser.add_argument(
153      '--artifact-url',
154      required=required,
155      type=str,
156      help=('Container URL. May be in the `gcr.io/repository/image` format,'
157            ' or may optionally contain the `http` or `https` scheme'))
158