Ejemplo n.º 1
0
    def __init__(self, base_url=None, auth_url=None, api_url=None, stream_url=None,
                 api_version=None, cacert=None, debug=False, token=None, api_key=None):
        # Get CLI options. If not given, then try to get it from the environment.
        self.endpoints = dict()

        # Populate the endpoints
        if base_url:
            self.endpoints['base'] = base_url
        else:
            self.endpoints['base'] = os.environ.get('ST2_BASE_URL', DEFAULT_BASE_URL)

        api_version = api_version or os.environ.get('ST2_API_VERSION', DEFAULT_API_VERSION)

        self.endpoints['exp'] = '%s:%s/%s' % (self.endpoints['base'], DEFAULT_API_PORT, 'exp')

        if api_url:
            self.endpoints['api'] = api_url
        else:
            self.endpoints['api'] = os.environ.get(
                'ST2_API_URL', '%s:%s/%s' % (self.endpoints['base'], DEFAULT_API_PORT, api_version))

        if auth_url:
            self.endpoints['auth'] = auth_url
        else:
            self.endpoints['auth'] = os.environ.get(
                'ST2_AUTH_URL', '%s:%s' % (self.endpoints['base'], DEFAULT_AUTH_PORT))

        if stream_url:
            self.endpoints['stream'] = stream_url
        else:
            self.endpoints['stream'] = os.environ.get(
                'ST2_STREAM_URL',
                '%s:%s/%s' % (
                    self.endpoints['base'],
                    DEFAULT_STREAM_PORT,
                    api_version
                )
            )

        if cacert is not None:
            self.cacert = cacert
        else:
            self.cacert = os.environ.get('ST2_CACERT', None)

        # Note: boolean is also a valid value for "cacert"
        is_cacert_string = isinstance(self.cacert, six.string_types)
        if (self.cacert and is_cacert_string and not os.path.isfile(self.cacert)):
            raise ValueError('CA cert file "%s" does not exist.' % (self.cacert))

        self.debug = debug

        # Note: This is a nasty hack for now, but we need to get rid of the decrator abuse
        if token:
            os.environ['ST2_AUTH_TOKEN'] = token

        self.token = token

        if api_key:
            os.environ['ST2_API_KEY'] = api_key

        self.api_key = api_key

        # Instantiate resource managers and assign appropriate API endpoint.
        self.managers = dict()
        self.managers['Token'] = ResourceManager(
            models.Token, self.endpoints['auth'], cacert=self.cacert, debug=self.debug)
        self.managers['RunnerType'] = ResourceManager(
            models.RunnerType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Action'] = ResourceManager(
            models.Action, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['ActionAlias'] = ActionAliasResourceManager(
            models.ActionAlias, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['ActionAliasExecution'] = ActionAliasExecutionManager(
            models.ActionAliasExecution, self.endpoints['api'],
            cacert=self.cacert, debug=self.debug)
        self.managers['ApiKey'] = ResourceManager(
            models.ApiKey, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Config'] = ConfigManager(
            models.Config, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['ConfigSchema'] = ResourceManager(
            models.ConfigSchema, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Execution'] = ExecutionResourceManager(
            models.Execution, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        # NOTE: LiveAction has been deprecated in favor of Execution. It will be left here for
        # backward compatibility reasons until v3.2.0
        self.managers['LiveAction'] = self.managers['Execution']
        self.managers['Inquiry'] = InquiryResourceManager(
            models.Inquiry, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Pack'] = PackResourceManager(
            models.Pack, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Policy'] = ResourceManager(
            models.Policy, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['PolicyType'] = ResourceManager(
            models.PolicyType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Rule'] = ResourceManager(
            models.Rule, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Sensor'] = ResourceManager(
            models.Sensor, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['TriggerType'] = ResourceManager(
            models.TriggerType, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Trigger'] = ResourceManager(
            models.Trigger, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['TriggerInstance'] = TriggerInstanceResourceManager(
            models.TriggerInstance, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['KeyValuePair'] = ResourceManager(
            models.KeyValuePair, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Webhook'] = WebhookManager(
            models.Webhook, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Timer'] = ResourceManager(
            models.Timer, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Trace'] = ResourceManager(
            models.Trace, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['RuleEnforcement'] = ResourceManager(
            models.RuleEnforcement, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['Stream'] = StreamManager(
            self.endpoints['stream'], cacert=self.cacert, debug=self.debug)
        self.managers['Workflow'] = WorkflowManager(
            self.endpoints['api'], cacert=self.cacert, debug=self.debug)

        # Service Registry
        self.managers['ServiceRegistryGroups'] = ServiceRegistryGroupsManager(
            models.ServiceRegistryGroup, self.endpoints['api'], cacert=self.cacert,
            debug=self.debug)

        self.managers['ServiceRegistryMembers'] = ServiceRegistryMembersManager(
            models.ServiceRegistryMember, self.endpoints['api'], cacert=self.cacert,
            debug=self.debug)

        # RBAC
        self.managers['Role'] = ResourceManager(
            models.Role, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
        self.managers['UserRoleAssignment'] = ResourceManager(
            models.UserRoleAssignment, self.endpoints['api'], cacert=self.cacert, debug=self.debug)
Ejemplo n.º 2
0
    def __init__(
        self,
        base_url=None,
        auth_url=None,
        api_url=None,
        stream_url=None,
        api_version=None,
        cacert=None,
        debug=False,
        token=None,
        api_key=None,
        basic_auth=None,
    ):
        # Get CLI options. If not given, then try to get it from the environment.
        self.endpoints = dict()

        # Populate the endpoints
        if base_url:
            self.endpoints["base"] = base_url
        else:
            self.endpoints["base"] = os.environ.get("ST2_BASE_URL",
                                                    DEFAULT_BASE_URL)

        api_version = api_version or os.environ.get("ST2_API_VERSION",
                                                    DEFAULT_API_VERSION)

        self.endpoints["exp"] = "%s:%s/%s" % (
            self.endpoints["base"],
            DEFAULT_API_PORT,
            "exp",
        )

        if api_url:
            self.endpoints["api"] = api_url
        else:
            self.endpoints["api"] = os.environ.get(
                "ST2_API_URL",
                "%s:%s/%s" %
                (self.endpoints["base"], DEFAULT_API_PORT, api_version),
            )

        if auth_url:
            self.endpoints["auth"] = auth_url
        else:
            self.endpoints["auth"] = os.environ.get(
                "ST2_AUTH_URL",
                "%s:%s" % (self.endpoints["base"], DEFAULT_AUTH_PORT))

        if stream_url:
            self.endpoints["stream"] = stream_url
        else:
            self.endpoints["stream"] = os.environ.get(
                "ST2_STREAM_URL",
                "%s:%s/%s" %
                (self.endpoints["base"], DEFAULT_STREAM_PORT, api_version),
            )

        if cacert is not None:
            self.cacert = cacert
        else:
            self.cacert = os.environ.get("ST2_CACERT", None)

        # Note: boolean is also a valid value for "cacert"
        is_cacert_string = isinstance(self.cacert, six.string_types)
        if self.cacert and is_cacert_string and not os.path.isfile(
                self.cacert):
            raise ValueError('CA cert file "%s" does not exist.' %
                             (self.cacert))

        self.debug = debug

        # Note: This is a nasty hack for now, but we need to get rid of the decrator abuse
        if token:
            os.environ["ST2_AUTH_TOKEN"] = token

        self.token = token

        if api_key:
            os.environ["ST2_API_KEY"] = api_key

        self.api_key = api_key

        if basic_auth:
            # NOTE: We assume username can't contain colons
            if len(basic_auth.split(":", 1)) != 2:
                raise ValueError(
                    "basic_auth config options needs to be in the "
                    "username:password notation")

            self.basic_auth = tuple(basic_auth.split(":", 1))
        else:
            self.basic_auth = None

        # Instantiate resource managers and assign appropriate API endpoint.
        self.managers = dict()
        self.managers["Token"] = ResourceManager(
            models.Token,
            self.endpoints["auth"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["RunnerType"] = ResourceManager(
            models.RunnerType,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Action"] = ActionResourceManager(
            models.Action,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["ActionAlias"] = ActionAliasResourceManager(
            models.ActionAlias,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["ActionAliasExecution"] = ActionAliasExecutionManager(
            models.ActionAliasExecution,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["ApiKey"] = ResourceManager(
            models.ApiKey,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Config"] = ConfigManager(
            models.Config,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["ConfigSchema"] = ResourceManager(
            models.ConfigSchema,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Execution"] = ExecutionResourceManager(
            models.Execution,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        # NOTE: LiveAction has been deprecated in favor of Execution. It will be left here for
        # backward compatibility reasons until v3.2.0
        self.managers["LiveAction"] = self.managers["Execution"]
        self.managers["Inquiry"] = InquiryResourceManager(
            models.Inquiry,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Pack"] = PackResourceManager(
            models.Pack,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Policy"] = ResourceManager(
            models.Policy,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["PolicyType"] = ResourceManager(
            models.PolicyType,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Rule"] = ResourceManager(
            models.Rule,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Sensor"] = ResourceManager(
            models.Sensor,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["TriggerType"] = ResourceManager(
            models.TriggerType,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Trigger"] = ResourceManager(
            models.Trigger,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["TriggerInstance"] = TriggerInstanceResourceManager(
            models.TriggerInstance,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["KeyValuePair"] = ResourceManager(
            models.KeyValuePair,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Webhook"] = WebhookManager(
            models.Webhook,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Timer"] = ResourceManager(
            models.Timer,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Trace"] = ResourceManager(
            models.Trace,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["RuleEnforcement"] = ResourceManager(
            models.RuleEnforcement,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Stream"] = StreamManager(
            self.endpoints["stream"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["Workflow"] = WorkflowManager(
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )

        # Service Registry
        self.managers["ServiceRegistryGroups"] = ServiceRegistryGroupsManager(
            models.ServiceRegistryGroup,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )

        self.managers[
            "ServiceRegistryMembers"] = ServiceRegistryMembersManager(
                models.ServiceRegistryMember,
                self.endpoints["api"],
                cacert=self.cacert,
                debug=self.debug,
                basic_auth=self.basic_auth,
            )

        # RBAC
        self.managers["Role"] = ResourceManager(
            models.Role,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )
        self.managers["UserRoleAssignment"] = ResourceManager(
            models.UserRoleAssignment,
            self.endpoints["api"],
            cacert=self.cacert,
            debug=self.debug,
            basic_auth=self.basic_auth,
        )