laceworksdk.api.v1.compliance

Lacework Compliance API wrapper.

  1# -*- coding: utf-8 -*-
  2"""
  3Lacework Compliance API wrapper.
  4"""
  5
  6import logging
  7
  8logger = logging.getLogger(__name__)
  9
 10
 11class ComplianceAPI:
 12    """
 13    Lacework Compliance API.
 14    """
 15
 16    def __init__(self, session):
 17        """
 18        Initializes the ComplianceAPI object.
 19
 20        :param session: An instance of the HttpSession class
 21
 22        :return ComplianceAPI object.
 23        """
 24
 25        super().__init__()
 26
 27        self._session = session
 28
 29    def _return_or_write(self,
 30                         file_format,
 31                         pdf_path,
 32                         response):
 33        """
 34        A method to either return JSON or write a file.
 35        """
 36
 37        if file_format == "json":
 38            return response.json()
 39        else:
 40            if pdf_path:
 41                logger.info(f"Creating PDF at {pdf_path}")
 42
 43                with open(pdf_path, "wb") as f:
 44                    f.write(response.content)
 45
 46                return response.content
 47            else:
 48                logger.error("A path must be provided when requesting a PDF formatted compliance report.")
 49
 50    def get_latest_aws_report(self,
 51                              aws_account_id,
 52                              file_format=None,
 53                              report_type=None,
 54                              pdf_path=None):
 55        """
 56        A method to get the latest compiance report for an AWS account.
 57
 58        :param aws_account_id: A string representing which AWS Account to query.
 59        :param file_format: A string representing the desired file format. ("pdf" or "json")
 60        :param report_type: A string representing the desired report type.
 61            ("AWS_CIS_S3", "NIST_800-53_Rev4", "ISO_2700", "HIPAA", "SOC", or "PCI")
 62        :param pdf_path: An absolute path for writing PDF compliance reports
 63
 64        :return response json
 65        """
 66
 67        logger.info("Getting latest AWS Compliance Report from Lacework...")
 68
 69        # Build the Compliance report request URI
 70        api_uri = "/api/v1/external/compliance/aws/GetLatestComplianceReport?" \
 71                  f"AWS_ACCOUNT_ID={aws_account_id}"
 72
 73        if file_format:
 74            api_uri += f"&FILE_FORMAT={file_format}"
 75
 76        if report_type:
 77            api_uri += f"&REPORT_TYPE={report_type}"
 78
 79        response = self._session.get(api_uri)
 80
 81        return self._return_or_write(file_format, pdf_path, response)
 82
 83    def get_latest_azure_report(self,
 84                                azure_tenant_id,
 85                                azure_subscription_id,
 86                                file_format=None,
 87                                report_type=None,
 88                                pdf_path=None):
 89        """
 90        A method to get the latest compiance report for an Azure tenant.
 91
 92        :param azure_tenant_id: A string representing which Azure Tenant to query.
 93        :param azure_subscription_id: A string representing which Azure Subscription to query.
 94        :param file_format: A string representing the desired file format. ("pdf" or "json")
 95        :param report_type: A string representing the desired report type.
 96            ("AZURE_CIS", "AZURE_SOC", or "AZURE_PCI")
 97        :param pdf_path: An absolute path for writing PDF compliance reports
 98
 99        :return response json
100        """
101
102        logger.info("Getting latest Azure Compliance Report from Lacework...")
103
104        # Build the Compliance report request URI
105        api_uri = "/api/v1/external/compliance/azure/GetLatestComplianceReport?" \
106                  f"AZURE_TENANT_ID={azure_tenant_id}&AZURE_SUBS_ID={azure_subscription_id}"
107
108        if file_format:
109            api_uri += f"&FILE_FORMAT={file_format}"
110
111        if report_type:
112            api_uri += f"&REPORT_TYPE={report_type}"
113
114        response = self._session.get(api_uri)
115
116        return self._return_or_write(file_format, pdf_path, response)
117
118    def get_latest_gcp_report(self,
119                              gcp_organization_id,
120                              gcp_project_id,
121                              file_format=None,
122                              report_type=None,
123                              pdf_path=None):
124        """
125        A method to get the latest compiance report for a Google Cloud organization.
126
127        :param gcp_organization_id: A string representing which GCP Organization to query.
128        :param gcp_project_id: A string representing which GCP Project to query.
129        :param file_format: A string representing the desired file format. ("pdf" or "json")
130        :param report_type: A string representing the desired report type.
131            ("GCP_CIS", "GCP_SOC", or "GCP_PCI")
132        :param pdf_path: An absolute path for writing PDF compliance reports
133
134        :return response json
135        """
136
137        logger.info("Getting latest GCP Compliance Report from Lacework...")
138
139        # Build the Compliance report request URI
140        api_uri = "/api/v1/external/compliance/gcp/GetLatestComplianceReport?" \
141                  f"GCP_ORG_ID={gcp_organization_id}&GCP_PROJ_ID={gcp_project_id}"
142
143        if file_format:
144            api_uri += f"&FILE_FORMAT={file_format}"
145
146        if report_type:
147            api_uri += f"&REPORT_TYPE={report_type}"
148
149        response = self._session.get(api_uri)
150
151        return self._return_or_write(file_format, pdf_path, response)
152
153    def list_azure_subscriptions(self,
154                                 azure_tenant_id):
155        """
156        A method to list the subscriptions in an Azure account.
157
158        :param azure_tenant_id: A string representing which Azure Tenant to query.
159
160        :return response json
161        """
162
163        logger.info("Getting list of Azure Subscriptions from Lacework...")
164
165        # Build the Compliance list subscription request URI
166        api_uri = "/api/v1/external/compliance/azure/ListSubscriptionsForTenant?" \
167                  f"AZURE_TENANT_ID={azure_tenant_id}"
168
169        response = self._session.get(api_uri)
170
171        return response.json()
172
173    def list_gcp_projects(self,
174                          gcp_organization_id):
175        """
176        A method to list the projects in a Google Cloud organization.
177
178        :param gcp_organization_id: A string representing which GCP Organization to query.
179
180        :return response json
181        """
182
183        logger.info("Getting list of GCP Projects from Lacework...")
184
185        # Build the Compliance list subscription request URI
186        api_uri = "/api/v1/external/compliance/gcp/ListProjectsForOrganization?" \
187                  f"GCP_ORG_ID={gcp_organization_id}"
188
189        response = self._session.get(api_uri)
190
191        return response.json()
class ComplianceAPI:
 12class ComplianceAPI:
 13    """
 14    Lacework Compliance API.
 15    """
 16
 17    def __init__(self, session):
 18        """
 19        Initializes the ComplianceAPI object.
 20
 21        :param session: An instance of the HttpSession class
 22
 23        :return ComplianceAPI object.
 24        """
 25
 26        super().__init__()
 27
 28        self._session = session
 29
 30    def _return_or_write(self,
 31                         file_format,
 32                         pdf_path,
 33                         response):
 34        """
 35        A method to either return JSON or write a file.
 36        """
 37
 38        if file_format == "json":
 39            return response.json()
 40        else:
 41            if pdf_path:
 42                logger.info(f"Creating PDF at {pdf_path}")
 43
 44                with open(pdf_path, "wb") as f:
 45                    f.write(response.content)
 46
 47                return response.content
 48            else:
 49                logger.error("A path must be provided when requesting a PDF formatted compliance report.")
 50
 51    def get_latest_aws_report(self,
 52                              aws_account_id,
 53                              file_format=None,
 54                              report_type=None,
 55                              pdf_path=None):
 56        """
 57        A method to get the latest compiance report for an AWS account.
 58
 59        :param aws_account_id: A string representing which AWS Account to query.
 60        :param file_format: A string representing the desired file format. ("pdf" or "json")
 61        :param report_type: A string representing the desired report type.
 62            ("AWS_CIS_S3", "NIST_800-53_Rev4", "ISO_2700", "HIPAA", "SOC", or "PCI")
 63        :param pdf_path: An absolute path for writing PDF compliance reports
 64
 65        :return response json
 66        """
 67
 68        logger.info("Getting latest AWS Compliance Report from Lacework...")
 69
 70        # Build the Compliance report request URI
 71        api_uri = "/api/v1/external/compliance/aws/GetLatestComplianceReport?" \
 72                  f"AWS_ACCOUNT_ID={aws_account_id}"
 73
 74        if file_format:
 75            api_uri += f"&FILE_FORMAT={file_format}"
 76
 77        if report_type:
 78            api_uri += f"&REPORT_TYPE={report_type}"
 79
 80        response = self._session.get(api_uri)
 81
 82        return self._return_or_write(file_format, pdf_path, response)
 83
 84    def get_latest_azure_report(self,
 85                                azure_tenant_id,
 86                                azure_subscription_id,
 87                                file_format=None,
 88                                report_type=None,
 89                                pdf_path=None):
 90        """
 91        A method to get the latest compiance report for an Azure tenant.
 92
 93        :param azure_tenant_id: A string representing which Azure Tenant to query.
 94        :param azure_subscription_id: A string representing which Azure Subscription to query.
 95        :param file_format: A string representing the desired file format. ("pdf" or "json")
 96        :param report_type: A string representing the desired report type.
 97            ("AZURE_CIS", "AZURE_SOC", or "AZURE_PCI")
 98        :param pdf_path: An absolute path for writing PDF compliance reports
 99
100        :return response json
101        """
102
103        logger.info("Getting latest Azure Compliance Report from Lacework...")
104
105        # Build the Compliance report request URI
106        api_uri = "/api/v1/external/compliance/azure/GetLatestComplianceReport?" \
107                  f"AZURE_TENANT_ID={azure_tenant_id}&AZURE_SUBS_ID={azure_subscription_id}"
108
109        if file_format:
110            api_uri += f"&FILE_FORMAT={file_format}"
111
112        if report_type:
113            api_uri += f"&REPORT_TYPE={report_type}"
114
115        response = self._session.get(api_uri)
116
117        return self._return_or_write(file_format, pdf_path, response)
118
119    def get_latest_gcp_report(self,
120                              gcp_organization_id,
121                              gcp_project_id,
122                              file_format=None,
123                              report_type=None,
124                              pdf_path=None):
125        """
126        A method to get the latest compiance report for a Google Cloud organization.
127
128        :param gcp_organization_id: A string representing which GCP Organization to query.
129        :param gcp_project_id: A string representing which GCP Project to query.
130        :param file_format: A string representing the desired file format. ("pdf" or "json")
131        :param report_type: A string representing the desired report type.
132            ("GCP_CIS", "GCP_SOC", or "GCP_PCI")
133        :param pdf_path: An absolute path for writing PDF compliance reports
134
135        :return response json
136        """
137
138        logger.info("Getting latest GCP Compliance Report from Lacework...")
139
140        # Build the Compliance report request URI
141        api_uri = "/api/v1/external/compliance/gcp/GetLatestComplianceReport?" \
142                  f"GCP_ORG_ID={gcp_organization_id}&GCP_PROJ_ID={gcp_project_id}"
143
144        if file_format:
145            api_uri += f"&FILE_FORMAT={file_format}"
146
147        if report_type:
148            api_uri += f"&REPORT_TYPE={report_type}"
149
150        response = self._session.get(api_uri)
151
152        return self._return_or_write(file_format, pdf_path, response)
153
154    def list_azure_subscriptions(self,
155                                 azure_tenant_id):
156        """
157        A method to list the subscriptions in an Azure account.
158
159        :param azure_tenant_id: A string representing which Azure Tenant to query.
160
161        :return response json
162        """
163
164        logger.info("Getting list of Azure Subscriptions from Lacework...")
165
166        # Build the Compliance list subscription request URI
167        api_uri = "/api/v1/external/compliance/azure/ListSubscriptionsForTenant?" \
168                  f"AZURE_TENANT_ID={azure_tenant_id}"
169
170        response = self._session.get(api_uri)
171
172        return response.json()
173
174    def list_gcp_projects(self,
175                          gcp_organization_id):
176        """
177        A method to list the projects in a Google Cloud organization.
178
179        :param gcp_organization_id: A string representing which GCP Organization to query.
180
181        :return response json
182        """
183
184        logger.info("Getting list of GCP Projects from Lacework...")
185
186        # Build the Compliance list subscription request URI
187        api_uri = "/api/v1/external/compliance/gcp/ListProjectsForOrganization?" \
188                  f"GCP_ORG_ID={gcp_organization_id}"
189
190        response = self._session.get(api_uri)
191
192        return response.json()

Lacework Compliance API.

ComplianceAPI(session)
17    def __init__(self, session):
18        """
19        Initializes the ComplianceAPI object.
20
21        :param session: An instance of the HttpSession class
22
23        :return ComplianceAPI object.
24        """
25
26        super().__init__()
27
28        self._session = session

Initializes the ComplianceAPI object.

Parameters
  • session: An instance of the HttpSession class

:return ComplianceAPI object.

def get_latest_aws_report( self, aws_account_id, file_format=None, report_type=None, pdf_path=None):
51    def get_latest_aws_report(self,
52                              aws_account_id,
53                              file_format=None,
54                              report_type=None,
55                              pdf_path=None):
56        """
57        A method to get the latest compiance report for an AWS account.
58
59        :param aws_account_id: A string representing which AWS Account to query.
60        :param file_format: A string representing the desired file format. ("pdf" or "json")
61        :param report_type: A string representing the desired report type.
62            ("AWS_CIS_S3", "NIST_800-53_Rev4", "ISO_2700", "HIPAA", "SOC", or "PCI")
63        :param pdf_path: An absolute path for writing PDF compliance reports
64
65        :return response json
66        """
67
68        logger.info("Getting latest AWS Compliance Report from Lacework...")
69
70        # Build the Compliance report request URI
71        api_uri = "/api/v1/external/compliance/aws/GetLatestComplianceReport?" \
72                  f"AWS_ACCOUNT_ID={aws_account_id}"
73
74        if file_format:
75            api_uri += f"&FILE_FORMAT={file_format}"
76
77        if report_type:
78            api_uri += f"&REPORT_TYPE={report_type}"
79
80        response = self._session.get(api_uri)
81
82        return self._return_or_write(file_format, pdf_path, response)

A method to get the latest compiance report for an AWS account.

Parameters
  • aws_account_id: A string representing which AWS Account to query.
  • file_format: A string representing the desired file format. ("pdf" or "json")
  • report_type: A string representing the desired report type. ("AWS_CIS_S3", "NIST_800-53_Rev4", "ISO_2700", "HIPAA", "SOC", or "PCI")
  • pdf_path: An absolute path for writing PDF compliance reports

:return response json

def get_latest_azure_report( self, azure_tenant_id, azure_subscription_id, file_format=None, report_type=None, pdf_path=None):
 84    def get_latest_azure_report(self,
 85                                azure_tenant_id,
 86                                azure_subscription_id,
 87                                file_format=None,
 88                                report_type=None,
 89                                pdf_path=None):
 90        """
 91        A method to get the latest compiance report for an Azure tenant.
 92
 93        :param azure_tenant_id: A string representing which Azure Tenant to query.
 94        :param azure_subscription_id: A string representing which Azure Subscription to query.
 95        :param file_format: A string representing the desired file format. ("pdf" or "json")
 96        :param report_type: A string representing the desired report type.
 97            ("AZURE_CIS", "AZURE_SOC", or "AZURE_PCI")
 98        :param pdf_path: An absolute path for writing PDF compliance reports
 99
100        :return response json
101        """
102
103        logger.info("Getting latest Azure Compliance Report from Lacework...")
104
105        # Build the Compliance report request URI
106        api_uri = "/api/v1/external/compliance/azure/GetLatestComplianceReport?" \
107                  f"AZURE_TENANT_ID={azure_tenant_id}&AZURE_SUBS_ID={azure_subscription_id}"
108
109        if file_format:
110            api_uri += f"&FILE_FORMAT={file_format}"
111
112        if report_type:
113            api_uri += f"&REPORT_TYPE={report_type}"
114
115        response = self._session.get(api_uri)
116
117        return self._return_or_write(file_format, pdf_path, response)

A method to get the latest compiance report for an Azure tenant.

Parameters
  • azure_tenant_id: A string representing which Azure Tenant to query.
  • azure_subscription_id: A string representing which Azure Subscription to query.
  • file_format: A string representing the desired file format. ("pdf" or "json")
  • report_type: A string representing the desired report type. ("AZURE_CIS", "AZURE_SOC", or "AZURE_PCI")
  • pdf_path: An absolute path for writing PDF compliance reports

:return response json

def get_latest_gcp_report( self, gcp_organization_id, gcp_project_id, file_format=None, report_type=None, pdf_path=None):
119    def get_latest_gcp_report(self,
120                              gcp_organization_id,
121                              gcp_project_id,
122                              file_format=None,
123                              report_type=None,
124                              pdf_path=None):
125        """
126        A method to get the latest compiance report for a Google Cloud organization.
127
128        :param gcp_organization_id: A string representing which GCP Organization to query.
129        :param gcp_project_id: A string representing which GCP Project to query.
130        :param file_format: A string representing the desired file format. ("pdf" or "json")
131        :param report_type: A string representing the desired report type.
132            ("GCP_CIS", "GCP_SOC", or "GCP_PCI")
133        :param pdf_path: An absolute path for writing PDF compliance reports
134
135        :return response json
136        """
137
138        logger.info("Getting latest GCP Compliance Report from Lacework...")
139
140        # Build the Compliance report request URI
141        api_uri = "/api/v1/external/compliance/gcp/GetLatestComplianceReport?" \
142                  f"GCP_ORG_ID={gcp_organization_id}&GCP_PROJ_ID={gcp_project_id}"
143
144        if file_format:
145            api_uri += f"&FILE_FORMAT={file_format}"
146
147        if report_type:
148            api_uri += f"&REPORT_TYPE={report_type}"
149
150        response = self._session.get(api_uri)
151
152        return self._return_or_write(file_format, pdf_path, response)

A method to get the latest compiance report for a Google Cloud organization.

Parameters
  • gcp_organization_id: A string representing which GCP Organization to query.
  • gcp_project_id: A string representing which GCP Project to query.
  • file_format: A string representing the desired file format. ("pdf" or "json")
  • report_type: A string representing the desired report type. ("GCP_CIS", "GCP_SOC", or "GCP_PCI")
  • pdf_path: An absolute path for writing PDF compliance reports

:return response json

def list_azure_subscriptions(self, azure_tenant_id):
154    def list_azure_subscriptions(self,
155                                 azure_tenant_id):
156        """
157        A method to list the subscriptions in an Azure account.
158
159        :param azure_tenant_id: A string representing which Azure Tenant to query.
160
161        :return response json
162        """
163
164        logger.info("Getting list of Azure Subscriptions from Lacework...")
165
166        # Build the Compliance list subscription request URI
167        api_uri = "/api/v1/external/compliance/azure/ListSubscriptionsForTenant?" \
168                  f"AZURE_TENANT_ID={azure_tenant_id}"
169
170        response = self._session.get(api_uri)
171
172        return response.json()

A method to list the subscriptions in an Azure account.

Parameters
  • azure_tenant_id: A string representing which Azure Tenant to query.

:return response json

def list_gcp_projects(self, gcp_organization_id):
174    def list_gcp_projects(self,
175                          gcp_organization_id):
176        """
177        A method to list the projects in a Google Cloud organization.
178
179        :param gcp_organization_id: A string representing which GCP Organization to query.
180
181        :return response json
182        """
183
184        logger.info("Getting list of GCP Projects from Lacework...")
185
186        # Build the Compliance list subscription request URI
187        api_uri = "/api/v1/external/compliance/gcp/ListProjectsForOrganization?" \
188                  f"GCP_ORG_ID={gcp_organization_id}"
189
190        response = self._session.get(api_uri)
191
192        return response.json()

A method to list the projects in a Google Cloud organization.

Parameters
  • gcp_organization_id: A string representing which GCP Organization to query.

:return response json