Exemple #1
0
    def put(self, source_id):
        """
        .. http:put:: /sources/1

           Updates an account

           **Example request**:

           .. sourcecode:: http

              POST /sources/1 HTTP/1.1
              Host: example.com
              Accept: application/json, text/javascript

              {
                "sourceOptions": [
                    {
                        "name": "accountNumber",
                        "required": true,
                        "value": 111111111112,
                        "helpMessage": "Must be a valid AWS account number!",
                        "validation": "/^[0-9]{12,12}$/",
                        "type": "int"
                    }
                ],
                "pluginName": "aws-source",
                "id": 3,
                "lastRun": "2015-08-01T15:40:58",
                "description": "test",
                "label": "test"
              }

           **Example response**:

           .. sourcecode:: http

              HTTP/1.1 200 OK
              Vary: Accept
              Content-Type: text/javascript

              {
                "sourceOptions": [
                    {
                        "name": "accountNumber",
                        "required": true,
                        "value": 111111111112,
                        "helpMessage": "Must be a valid AWS account number!",
                        "validation": "/^[0-9]{12,12}$/",
                        "type": "int"
                    }
                ],
                "pluginName": "aws-source",
                "id": 3,
                "lastRun": "2015-08-01T15:40:58",
                "description": "test",
                "label": "test"
              }

           :arg accountNumber: aws account number
           :arg label: human readable account label
           :arg description: some description about the account
           :reqheader Authorization: OAuth token to authenticate
           :statuscode 200: no error
        """
        self.reqparse.add_argument('label', type=str, location='json', required=True)
        self.reqparse.add_argument('plugin', type=dict, location='json', required=True)
        self.reqparse.add_argument('description', type=str, location='json')

        args = self.reqparse.parse_args()
        return service.update(source_id, args['label'], args['plugin']['pluginOptions'], args['description'])
Exemple #2
0
    def put(self, source_id, data=None):
        """
        .. http:put:: /sources/1

           Updates an account

           **Example request**:

           .. sourcecode:: http

              POST /sources/1 HTTP/1.1
              Host: example.com
              Accept: application/json, text/javascript
              Content-Type: application/json;charset=UTF-8

              {
                "options": [
                    {
                        "name": "accountNumber",
                        "required": true,
                        "value": 111111111112,
                        "helpMessage": "Must be a valid AWS account number!",
                        "validation": "^[0-9]{12,12}$",
                        "type": "int"
                    }
                ],
                "pluginName": "aws-source",
                "id": 3,
                "lastRun": "2015-08-01T15:40:58",
                "description": "test",
                "label": "test"
              }

           **Example response**:

           .. sourcecode:: http

              HTTP/1.1 200 OK
              Vary: Accept
              Content-Type: text/javascript

              {
                "options": [
                    {
                        "name": "accountNumber",
                        "required": true,
                        "value": 111111111112,
                        "helpMessage": "Must be a valid AWS account number!",
                        "validation": "^[0-9]{12,12}$",
                        "type": "int"
                    }
                ],
                "pluginName": "aws-source",
                "id": 3,
                "lastRun": "2015-08-01T15:40:58",
                "description": "test",
                "label": "test"
              }

           :arg accountNumber: aws account number
           :arg label: human readable account label
           :arg description: some description about the account
           :reqheader Authorization: OAuth token to authenticate
           :statuscode 200: no error
        """
        return service.update(
            source_id,
            data["label"],
            data["plugin"]["slug"],
            data["plugin"]["plugin_options"],
            data["description"],
        )
Exemple #3
0
def enable_cloudfront(source_label):
    """
    Given the label of a legacy AWS source (without path or endpointType options), set up the source for CloudFront:

    #. Update the source options to the newest template, inheriting the existing values.
    #. Set ``path`` to "/" and ``endpointType`` to "elb" to restrict the source to discovering ELBs and related certs only.
    #. Create a new source (and destination) for the same accountNumber with ``path`` as "/cloudfront/" and ``endpointType`` as "cloudfront"

    :param source_strings:
    :return:
    """
    class ValidationError(Exception):
        pass

    try:
        source = source_service.get_by_label(source_label)
        if not source:
            raise ValidationError(
                f"Unable to find source with label: {source_label}")
        if source.plugin_name != "aws-source":
            raise ValidationError(
                f"Source '{source_label}' is not an AWS source")
        for opt_name in ["endpointType", "path"]:
            if get_plugin_option(opt_name, source.options) is not None:
                raise ValidationError(
                    f"Source '{source_label}' already sets option '{opt_name}'"
                )
        cloudfront_label = f"{source_label}-cloudfront"
        cloudfront_source = source_service.get_by_label(cloudfront_label)
        if cloudfront_source:
            raise ValidationError(
                f"A source named '{cloudfront_label}' already exists")

        p = plugins.get(source.plugin_name)
        new_options = deepcopy(p.options)
        for old_opt in source.options:
            name = old_opt["name"]
            value = get_plugin_option(name, source.options)
            set_plugin_option(name, value, new_options)
        set_plugin_option("path", "/", new_options)
        set_plugin_option("endpointType", "elb", new_options)
        source_service.update(source.id, source.label, source.plugin_name,
                              new_options, source.description)

        cloudfront_options = deepcopy(new_options)
        set_plugin_option("path", "/cloudfront/", cloudfront_options)
        set_plugin_option("endpointType", "cloudfront", cloudfront_options)
        source_service.create(
            cloudfront_label, source.plugin_name, cloudfront_options,
            f"CloudFront certificates and distributions for {source_label}")

        print(
            f"[+] Limited source {source_label} to discover ELBs and ELB certificates.\n"
        )
        print(
            f"[+] Created source {cloudfront_label} to discover CloudFront distributions and certificates.\n"
        )

    except ValidationError as e:
        print(f"[+] Error: {str(e)}")
        sys.exit(1)