Skip to content

Commit 6f982e7

Browse files
committed
Code which still needs testing
to enable ACS deployment. reduce non-blocking warnings thrown by appinspect api by updating some automatically generated app files.
1 parent a169fee commit 6f982e7

File tree

12 files changed

+211
-196
lines changed

12 files changed

+211
-196
lines changed

contentctl/actions/acs_deploy.py

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,55 @@
1-
from dataclasses import dataclass
2-
from contentctl.input.director import DirectorInputDto
3-
from contentctl.output.conf_output import ConfOutput
4-
5-
6-
from typing import Union
7-
8-
@dataclass(frozen=True)
9-
class ACSDeployInputDto:
10-
director_input_dto: DirectorInputDto
11-
splunk_api_username: str
12-
splunk_api_password: str
13-
splunk_cloud_jwt_token: str
14-
splunk_cloud_stack: str
15-
stack_type: str
1+
from contentctl.objects.config import deploy_acs, StackType
2+
from requests import post
3+
import pprint
164

175

186
class Deploy:
19-
def execute(self, input_dto: ACSDeployInputDto) -> None:
7+
def execute(self, config: deploy_acs, appinspect_token:str) -> None:
208

21-
conf_output = ConfOutput(input_dto.director_input_dto.input_path, input_dto.director_input_dto.config)
9+
#The following common headers are used by both Clasic and Victoria
10+
headers = {
11+
'Authorization': f'Bearer {config.splunk_cloud_jwt_token}',
12+
'ACS-Legal-Ack': 'Y'
13+
}
14+
try:
15+
16+
with open(config.getPackageFilePath(include_version=False),'rb') as app_data:
17+
#request_data = app_data.read()
18+
if config.stack_type == StackType.classic:
19+
# Classic instead uses a form to store token and package
20+
# https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/Config/ManageApps#Manage_private_apps_using_the_ACS_API_on_Classic_Experience
21+
address = f"https://admin.splunk.com/{config.splunk_cloud_stack}/adminconfig/v2/apps"
22+
23+
form_data = {
24+
'token': (None, appinspect_token),
25+
'package': app_data
26+
}
27+
res = post(address, headers=headers, files = form_data)
28+
elif config.stack_type == StackType.victoria:
29+
# Victoria uses the X-Splunk-Authorization Header
30+
# It also uses --data-binary for the app content
31+
# https://docs.splunk.com/Documentation/SplunkCloud/9.1.2308/Config/ManageApps#Manage_private_apps_using_the_ACS_API_on_Victoria_Experience
32+
headers.update({'X-Splunk-Authorization': appinspect_token})
33+
address = f"https://admin.splunk.com/{config.splunk_cloud_stack}/adminconfig/v2/apps/victoria"
34+
res = post(address, headers=headers, data=app_data.read())
35+
else:
36+
raise Exception(f"Unsupported stack type: '{config.stack_type}'")
37+
except Exception as e:
38+
raise Exception(f"Error installing to stack '{config.splunk_cloud_stack}' (stack_type='{config.stack_type}') via ACS:\n{str(e)}")
2239

23-
appinspect_token = conf_output.inspectAppAPI(input_dto.splunk_api_username, input_dto.splunk_api_password, input_dto.stack_type)
24-
25-
26-
if input_dto.splunk_cloud_jwt_token is None or input_dto.splunk_cloud_stack is None:
27-
if input_dto.splunk_cloud_jwt_token is None:
28-
raise Exception("Cannot deploy app via ACS, --splunk_cloud_jwt_token was not defined on command line.")
29-
else:
30-
raise Exception("Cannot deploy app via ACS, --splunk_cloud_stack was not defined on command line.")
40+
try:
41+
# Request went through and completed, but may have returned a non-successful error code.
42+
# This likely includes a more verbose response describing the error
43+
res.raise_for_status()
44+
except Exception as e:
45+
try:
46+
error_text = res.json()
47+
except Exception as e:
48+
error_text = "No error text - request failed"
49+
formatted_error_text = pprint.pformat(error_text)
50+
raise Exception(f"Error installing to stack '{config.splunk_cloud_stack}' (stack_type='{config.stack_type}') via ACS:\n{formatted_error_text}")
3151

32-
conf_output.deploy_via_acs(input_dto.splunk_cloud_jwt_token,
33-
input_dto.splunk_cloud_stack,
34-
appinspect_token,
35-
input_dto.stack_type)
52+
print(f"'{config.getPackageFilePath(include_version=False)}' successfully installed to stack '{config.splunk_cloud_stack}' (stack_type='{config.stack_type}') via ACS!")
3653

3754

3855

contentctl/actions/apav_deploy.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

contentctl/actions/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def execute(self, input_dto: BuildInputDto) -> DirectorOutputDto:
3838
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.investigations, SecurityContentType.investigations))
3939
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.lookups, SecurityContentType.lookups))
4040
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.macros, SecurityContentType.macros))
41-
updated_conf_files.update(conf_output.writeAppConf())
41+
updated_conf_files.update(conf_output.writeMiscellaneousAppFiles())
4242

4343
#Ensure that the conf file we just generated/update is syntactically valid
4444
for conf_file in updated_conf_files:

contentctl/contentctl.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from contentctl.actions.test import TestInputDto
1515
from contentctl.actions.reporting import ReportingInputDto, Reporting
1616
from contentctl.actions.inspect import Inspect
17+
from contentctl.actions.acs_deploy import Deploy
1718
import sys
1819
import warnings
1920
import pathlib
@@ -81,8 +82,8 @@ def build_func(config:build)->DirectorOutputDto:
8182
def inspect_func(config:inspect)->str:
8283
#Make sure that we have built the most recent version of the app
8384
_ = build_func(config)
84-
inspect_token = Inspect().execute(config)
85-
return inspect_token
85+
appinspect_token = Inspect().execute(config)
86+
return appinspect_token
8687

8788

8889
def release_notes_func(config:release_notes)->None:
@@ -94,8 +95,10 @@ def new_func(config:new):
9495

9596

9697
def deploy_acs_func(config:deploy_acs):
97-
#This is a bit challenging to get to work with the default values.
98-
raise Exception("deploy acs not yet implemented")
98+
appinspect_token = inspect_func(config)
99+
Deploy().execute(config, appinspect_token)
100+
101+
99102

100103
def deploy_rest_func(config:deploy_rest):
101104
raise Exception("deploy rest not yet implemented")
@@ -175,7 +178,7 @@ def main():
175178
"test":test.model_validate(config_obj),
176179
"test_servers":test_servers.model_construct(**t.__dict__),
177180
"release_notes": release_notes.model_construct(**config_obj),
178-
"deploy_acs": deploy_acs.model_construct(**t.__dict__),
181+
"deploy_acs": deploy_acs.model_construct(**config_obj),
179182
#"deploy_rest":deploy_rest()
180183
}
181184
)

contentctl/objects/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class StackType(StrEnum):
238238
victoria = auto()
239239

240240
class inspect(build):
241-
splunk_api_username: str = Field(description="Splunk API username used for running appinspect.")
241+
splunk_api_username: str = Field(exclude=True, description="Splunk API username used for running appinspect.")
242242
splunk_api_password: str = Field(exclude=True, description="Splunk API password used for running appinspect.")
243243
stack_type: StackType = Field(description="The type of your Splunk Cloud Stack")
244244

contentctl/output/conf_output.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,26 @@ def writeHeaders(self) -> set[pathlib.Path]:
5757
pass
5858

5959

60-
def writeAppConf(self)->set[pathlib.Path]:
60+
61+
62+
def writeMiscellaneousAppFiles(self)->set[pathlib.Path]:
6163
written_files:set[pathlib.Path] = set()
62-
for output_app_path, template_name in [ ("default/app.conf", "app.conf.j2"),
63-
("default/content-version.conf", "content-version.j2")]:
64-
written_files.add(ConfWriter.writeConfFile(pathlib.Path(output_app_path),
65-
template_name,
66-
self.config,
67-
[self.config.app]))
64+
65+
written_files.add(ConfWriter.writeConfFile(pathlib.Path("content-version.conf"),
66+
"content-version.j2",
67+
self.config,
68+
[self.config.app]))
6869

6970
written_files.add(ConfWriter.writeManifestFile(pathlib.Path("app.manifest"),
7071
"app.manifest.j2",
7172
self.config,
7273
[self.config.app]))
74+
75+
written_files.add(ConfWriter.writeServerConf(self.config))
76+
77+
written_files.add(ConfWriter.writeAppConf(self.config))
78+
79+
7380
return written_files
7481

7582

0 commit comments

Comments
 (0)