Example #1
0
def _ParseCAResourceArgs(args):
  """Parses, validates and returns the resource args from the CLI.

  Args:
    args: The parsed arguments from the command-line.

  Returns:
    Tuple containing the Resource objects for (CA, source CA, issuer).
  """
  resource_args.ValidateResourceIsCompleteIfSpecified(args, 'kms_key_version')
  resource_args.ValidateResourceIsCompleteIfSpecified(args, 'issuer')
  resource_args.ValidateResourceIsCompleteIfSpecified(args, 'from_ca')

  ca_ref = args.CONCEPTS.certificate_authority.Parse()
  resource_args.ValidateResourceLocation(ca_ref, 'CERTIFICATE_AUTHORITY')

  kms_key_version_ref = args.CONCEPTS.kms_key_version.Parse()
  if kms_key_version_ref and ca_ref.locationsId != kms_key_version_ref.locationsId:
    raise exceptions.InvalidArgumentException(
        '--kms-key-version',
        'KMS key must be in the same location as the Certificate Authority '
        '({}).'.format(ca_ref.locationsId))

  issuer_ref = args.CONCEPTS.issuer.Parse() if hasattr(args, 'issuer') else None
  source_ca_ref = args.CONCEPTS.from_ca.Parse()

  return (ca_ref, source_ca_ref, issuer_ref)
Example #2
0
 def testAcceptsFullResourceName(self):
     self._AddCaResourceArg(required=False, prefixes=True)
     parsed_args = self.parser.parse_args([
         '--issuer=projects/foo/locations/us-west1/certificateAuthorities/ca'
     ])
     resource_args.ValidateResourceIsCompleteIfSpecified(
         parsed_args, 'issuer')
Example #3
0
 def testFailsOnMalformedResource(self):
     self._AddCaResourceArg(required=False, prefixes=True)
     parsed_args = self.parser.parse_args([
         '--issuer=projects/foo/locations/us-west1/certificateAuthorities'
     ])
     with self.assertRaisesRegex(
             handlers.ParseError,
             r'The \[Issuer\] resource is not properly specified\.'):
         resource_args.ValidateResourceIsCompleteIfSpecified(
             parsed_args, 'issuer')
Example #4
0
    def _ValidateArgs(self, args):
        """Validates the command-line args."""
        if args.IsSpecified('use_preset_profile') and args.IsSpecified(
                'template'):
            raise exceptions.OneOfArgumentsRequiredException([
                '--use-preset-profile', '--template'
            ], ('To create a certificate, please specify either a preset profile '
                'or a certificate template.'))

        resource_args.ValidateResourceIsCompleteIfSpecified(
            args, 'kms_key_version')
Example #5
0
    def testFailsOnMissingLocation(self):
        self._AddCaResourceArg(required=False, prefixes=True)
        parsed_args = self.parser.parse_args(['--issuer=ca'])
        expected_error = '''The [Issuer] resource is not properly specified.
Failed to find attribute [location]. The attribute can be set in the following ways:
- provide the argument [--issuer-location] on the command line
- set the property [privateca/location]
'''
        with self.assertRaises(handlers.ParseError, msg=expected_error):
            resource_args.ValidateResourceIsCompleteIfSpecified(
                parsed_args, 'issuer')
Example #6
0
 def testAcceptsPropertyFallthroughs(self):
     properties.VALUES.privateca.location.Set('us-west1')
     self._AddCaResourceArg(required=False, prefixes=True)
     parsed_args = self.parser.parse_args(['--issuer=ca'])
     resource_args.ValidateResourceIsCompleteIfSpecified(
         parsed_args, 'issuer')
Example #7
0
 def testAcceptsFullComponentizedResource(self):
     self._AddCaResourceArg(required=False, prefixes=True)
     parsed_args = self.parser.parse_args(
         ['--issuer=ca', '--issuer-location=us-west1'])
     resource_args.ValidateResourceIsCompleteIfSpecified(
         parsed_args, 'issuer')
Example #8
0
 def testIgnoresMissingOptionalArgs(self):
     self._AddCaResourceArg(required=False, prefixes=True)
     parsed_args = self.parser.parse_args([])
     resource_args.ValidateResourceIsCompleteIfSpecified(
         parsed_args, 'issuer')
Example #9
0
 def testIgnoresUnknownArgs(self):
     parsed_args = self.parser.parse_args([])
     resource_args.ValidateResourceIsCompleteIfSpecified(
         parsed_args, 'issuer')