Skip to content

Commit 17a258a

Browse files
committed
feat(auth): Add SCM authentication support for repository access
1 parent d55ec7f commit 17a258a

12 files changed

+268
-126
lines changed

examples/anthropic_tool_use.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/usr/bin/env python
1+
# export ANTHROPIC_API_KEY=...
2+
# python -m examples.anthropic_tool_use
23

34
from __future__ import annotations
45

@@ -11,6 +12,8 @@
1112
from gitpod import AsyncGitpod
1213
from gitpod.types.environment_initializer_param import Spec
1314

15+
from .scm_auth import verify_context_url # type: ignore
16+
1417
gpclient = AsyncGitpod()
1518
llmclient = Anthropic()
1619

@@ -41,8 +44,7 @@ async def create_environment(args: dict[str, str], cleanup: util.Disposables) ->
4144
env_class = await util.find_most_used_environment_class(gpclient)
4245
if not env_class:
4346
raise Exception("No environment class found. Please create one first.")
44-
env_class_id = env_class.id
45-
assert env_class_id is not None
47+
await verify_context_url(gpclient, args["context_url"], env_class.runner_id)
4648

4749
environment = (await gpclient.environments.create(
4850
spec={
@@ -54,18 +56,15 @@ async def create_environment(args: dict[str, str], cleanup: util.Disposables) ->
5456
}
5557
)]},
5658
},
57-
"machine": {"class": env_class_id},
59+
"machine": {"class": env_class.id},
5860
}
5961
)).environment
60-
assert environment is not None
61-
environment_id = environment.id
62-
assert environment_id is not None
63-
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
62+
cleanup.adda(lambda: gpclient.environments.delete(environment_id=environment.id))
6463

65-
print(f"\nCreated environment: {environment_id} - waiting for it to be ready...")
66-
await util.wait_for_environment_ready(gpclient, environment_id)
67-
print(f"\nEnvironment is ready: {environment_id}")
68-
return environment_id
64+
print(f"\nCreated environment: {environment.id} - waiting for it to be ready...")
65+
await util.wait_for_environment_running(gpclient, environment.id)
66+
print(f"\nEnvironment is ready: {environment.id}")
67+
return environment.id
6968

7069
async def execute_command(args: dict[str, str]) -> str:
7170
lines_iter = await util.run_command(gpclient, args["environment_id"], args["command"])
@@ -135,6 +134,4 @@ async def main(cleanup: util.Disposables) -> None:
135134

136135
if __name__ == "__main__":
137136
import asyncio
138-
disposables = util.Disposables()
139-
with disposables:
140-
asyncio.run(main(disposables))
137+
asyncio.run(util.with_disposables(main))

examples/fs_access.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
import sys
42
import asyncio
53
from io import StringIO
@@ -11,10 +9,12 @@
119
from gitpod.types.environment_spec_param import EnvironmentSpecParam
1210
from gitpod.types.environment_initializer_param import Spec
1311

12+
from .scm_auth import verify_context_url # type: ignore
13+
1414

1515
# Examples:
16-
# - ./examples/fs_access.py
17-
# - ./examples/fs_access.py https://github.com/gitpod-io/empty
16+
# - python -m examples.fs_access
17+
# - python -m examples.fs_access https://github.com/gitpod-io/empty
1818
async def main(cleanup: util.Disposables) -> None:
1919
client = AsyncGitpod()
2020

@@ -25,8 +25,6 @@ async def main(cleanup: util.Disposables) -> None:
2525
print("Error: No environment class found. Please create one first.")
2626
sys.exit(1)
2727
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
28-
env_class_id = env_class.id
29-
assert env_class_id is not None
3028

3129
print("Generating SSH key pair")
3230
key = paramiko.RSAKey.generate(2048)
@@ -39,13 +37,14 @@ async def main(cleanup: util.Disposables) -> None:
3937
key_id = "fs-access-example"
4038
spec: EnvironmentSpecParam = {
4139
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
42-
"machine": {"class": env_class_id},
40+
"machine": {"class": env_class.id},
4341
"ssh_public_keys": [{
4442
"id": key_id,
4543
"value": public_key
4644
}]
4745
}
4846
if context_url:
47+
await verify_context_url(client, context_url, env_class.runner_id)
4948
spec["content"] = {
5049
"initializer": {"specs": [Spec(
5150
context_url={
@@ -56,13 +55,10 @@ async def main(cleanup: util.Disposables) -> None:
5655

5756
print("Creating environment")
5857
environment = (await client.environments.create(spec=spec)).environment
59-
assert environment is not None
60-
environment_id = environment.id
61-
assert environment_id is not None
62-
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
58+
cleanup.adda(lambda: client.environments.delete(environment_id=environment.id))
6359

64-
env = util.EnvironmentState(client, environment_id)
65-
cleanup.add(lambda: asyncio.run(env.close()))
60+
env = util.EnvironmentState(client, environment.id)
61+
cleanup.adda(lambda: env.close())
6662

6763
print("Waiting for environment to be running")
6864
await env.wait_until_running()
@@ -104,6 +100,4 @@ async def main(cleanup: util.Disposables) -> None:
104100
print(f"File content: {content.decode()}")
105101

106102
if __name__ == "__main__":
107-
disposables = util.Disposables()
108-
with disposables:
109-
asyncio.run(main(disposables))
103+
asyncio.run(util.with_disposables(main))

examples/run_command.py

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
import sys
42
import asyncio
53

@@ -8,10 +6,12 @@
86
from gitpod.types.environment_spec_param import EnvironmentSpecParam
97
from gitpod.types.environment_initializer_param import Spec
108

9+
from .scm_auth import verify_context_url # type: ignore
10+
1111

1212
# Examples:
13-
# - ./examples/run_command.py 'echo "Hello World!"'
14-
# - ./examples/run_command.py 'echo "Hello World!"' https://github.com/gitpod-io/empty
13+
# - python -m examples.run_command 'echo "Hello World!"'
14+
# - python -m examples.run_command 'echo "Hello World!"' https://github.com/gitpod-io/empty
1515
async def main(cleanup: util.Disposables) -> None:
1616
client = AsyncGitpod()
1717

@@ -27,14 +27,13 @@ async def main(cleanup: util.Disposables) -> None:
2727
print("Error: No environment class found. Please create one first.")
2828
sys.exit(1)
2929
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
30-
env_class_id = env_class.id
31-
assert env_class_id is not None
32-
30+
3331
spec: EnvironmentSpecParam = {
3432
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
35-
"machine": {"class": env_class_id},
33+
"machine": {"class": env_class.id},
3634
}
3735
if context_url:
36+
await verify_context_url(client, context_url, env_class.runner_id)
3837
spec["content"] = {
3938
"initializer": {"specs": [Spec(
4039
context_url={
@@ -45,20 +44,15 @@ async def main(cleanup: util.Disposables) -> None:
4544

4645
print("Creating environment")
4746
environment = (await client.environments.create(spec=spec)).environment
48-
assert environment is not None
49-
environment_id = environment.id
50-
assert environment_id is not None
51-
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
47+
cleanup.adda(lambda: client.environments.delete(environment_id=environment.id))
5248

5349
print("Waiting for environment to be ready")
54-
await util.wait_for_environment_ready(client, environment_id)
50+
await util.wait_for_environment_running(client, environment.id)
5551

5652
print("Running command")
57-
lines = await util.run_command(client, environment_id, command)
53+
lines = await util.run_command(client, environment.id, command)
5854
async for line in lines:
5955
print(line)
6056

6157
if __name__ == "__main__":
62-
disposables = util.Disposables()
63-
with disposables:
64-
asyncio.run(main(disposables))
58+
asyncio.run(util.with_disposables(main))

examples/run_service.py

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python
2-
31
import sys
42
import asyncio
53

@@ -8,56 +6,53 @@
86
from gitpod.types.environment_spec_param import EnvironmentSpecParam
97
from gitpod.types.environment_initializer_param import Spec
108

9+
from .scm_auth import verify_context_url # type: ignore
10+
1111

1212
# Examples:
13-
# - ./examples/run_service.py
14-
# - ./examples/run_service.py https://github.com/gitpod-io/empty
13+
# - python -m examples.run_service
14+
# - python -m examples.run_service https://github.com/gitpod-io/empty
1515
async def main(cleanup: util.Disposables) -> None:
1616
client = AsyncGitpod()
1717

1818
context_url = sys.argv[1] if len(sys.argv) > 1 else None
19-
19+
2020
env_class = await util.find_most_used_environment_class(client)
2121
if not env_class:
2222
print("Error: No environment class found. Please create one first.")
2323
sys.exit(1)
2424
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
25-
env_class_id = env_class.id
26-
assert env_class_id is not None
2725

2826
port = 8888
2927
spec: EnvironmentSpecParam = {
3028
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
31-
"machine": {"class": env_class_id},
29+
"machine": {"class": env_class.id},
3230
"ports": [{
3331
"name": "Lama Service",
3432
"port": port,
3533
"admission": "ADMISSION_LEVEL_EVERYONE"
3634
}]
3735
}
3836
if context_url:
37+
await verify_context_url(client, context_url, env_class.runner_id)
3938
spec["content"] = {
4039
"initializer": {"specs": [Spec(
41-
context_url={
42-
"url": context_url
43-
}
44-
)]}
45-
}
40+
context_url={
41+
"url": context_url
42+
}
43+
)]}
44+
}
4645

47-
print("Creating environment")
4846
environment = (await client.environments.create(spec=spec)).environment
49-
assert environment is not None
50-
environment_id = environment.id
51-
assert environment_id is not None
52-
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
47+
cleanup.adda(lambda: client.environments.delete(environment_id=environment.id))
5348

5449
print("Waiting for environment to be ready")
55-
env = util.EnvironmentState(client, environment_id)
56-
cleanup.add(lambda: asyncio.run(env.close()))
50+
env = util.EnvironmentState(client, environment.id)
51+
cleanup.adda(lambda: env.close())
5752
await env.wait_until_running()
5853

5954
print("Starting Lama Service")
60-
lines = await util.run_service(client, environment_id, {
55+
lines = await util.run_service(client, environment.id, {
6156
"name":"Lama Service",
6257
"description":"Lama Service",
6358
"reference":"lama-service"
@@ -75,6 +70,4 @@ async def main(cleanup: util.Disposables) -> None:
7570
print(line)
7671

7772
if __name__ == "__main__":
78-
disposables = util.Disposables()
79-
with disposables:
80-
asyncio.run(main(disposables))
73+
asyncio.run(util.with_disposables(main))

0 commit comments

Comments
 (0)