Skip to content

Commit 289ceba

Browse files
authored
Improve secret handling (#23)
Use AWS secret manager to store secrets. The best way to set this up is by attaching the right role to the AWS instance.
1 parent f45b305 commit 289ceba

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

app/serve.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,29 @@
1919
SLACK_BOT_TOKEN,
2020
)
2121

22+
23+
def get_secret(secret_name):
24+
aws_secret_id = os.environ.get("RAY_ASSISTANT_AWS_SECRET_ID")
25+
if aws_secret_id:
26+
import boto3
27+
client = boto3.client(
28+
"secretsmanager", region_name=os.environ["RAY_ASSISTANT_AWS_REGION"]
29+
)
30+
response = client.get_secret_value(SecretId=aws_secret_id)
31+
return json.loads(response["SecretString"])[secret_name]
32+
else:
33+
raise NotImplemented(
34+
"Currently only AWS is supported "
35+
"and you need to set RAY_ASSISTANT_AWS_SECRET_ID")
36+
37+
2238
app = FastAPI()
2339

2440

2541
@ray.remote
2642
class SlackApp:
2743
def __init__(self):
28-
slack_app = App(token=os.environ["SLACK_BOT_TOKEN"])
44+
slack_app = App(token=get_secret("SLACK_BOT_TOKEN"))
2945

3046
@slack_app.event("app_mention")
3147
def event_mention(body, say):
@@ -39,16 +55,14 @@ def event_mention(body, say):
3955
self.slack_app = slack_app
4056

4157
def run(self):
42-
SocketModeHandler(self.slack_app, SLACK_APP_TOKEN).start()
58+
SocketModeHandler(self.slack_app, get_secret("SLACK_APP_TOKEN")).start()
4359

4460

4561
ray.init(
4662
runtime_env={
4763
"env_vars": {
48-
"DB_CONNECTION_STRING": DB_CONNECTION_STRING,
64+
"DB_CONNECTION_STRING": get_secret("DB_CONNECTION_STRING"),
4965
"OPENAI_API_KEY": OPENAI_API_KEY,
50-
"SLACK_APP_TOKEN": SLACK_APP_TOKEN,
51-
"SLACK_BOT_TOKEN": SLACK_BOT_TOKEN,
5266
}
5367
},
5468
ignore_reinit_error=True,

app/service.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: "ray-assistant"
2-
cluster_env: ray-assistant
2+
cluster_env: ray-assistant:2
33
ray_serve_config:
44
import_path: app.serve:deployment
55
runtime_env:
6-
working_dir: "https://github.com/ray-project/llm-applications/archive/refs/tags/v0.0.1.zip"
6+
working_dir: "https://github.com/ray-project/llm-applications/archive/refs/tags/v0.0.2.zip"
7+
env_vars: {
8+
RAY_ASSISTANT_AWS_SECRET_ID: "ray-assistant",
9+
RAY_ASSISTANT_AWS_REGION: "us-west-2"
10+
}

0 commit comments

Comments
 (0)