Skip to content

Commit f5ddd97

Browse files
committed
Add docs for HTTP basic auth
1 parent 281fb79 commit f5ddd97

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

README.md

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,71 @@ Docker image `sourcegraph/lang-python` from Docker Hub.
1919

2020
### 🔐 Secure deployment 🔐
2121

22-
We recommend deploying the language server behind an auth proxy or firewall and
23-
treating it like an authenticated user because anyone that connects to the
24-
language server can access resources such as private code that the language
25-
server has access to.
26-
27-
Make sure you set `python.sourcegraphUrl` to the URL that the language
28-
server should use to reach Sourcegraph, which is likely different from the URL
29-
that end users use.
22+
If you have private code, we recommend deploying the language server behind an
23+
auth proxy (such as the example below using HTTP basic authentication in NGINX), a firewall, or a VPN.
24+
25+
### HTTP basic authentication
26+
27+
You can prevent unauthorized access to the language server by enforcing HTTP basic authentication in nginx, which comes with the sourcegraph/server image. At a high level, you'll create a secret then put it in both the nginx config and in your Sourcegraph global settings so that logged-in users are authenticated when their browser makes requests to the Python language server.
28+
29+
Here's how to set it up:
30+
31+
Create an `.htpasswd` file in the Sourcegraph config directory with one entry:
32+
33+
```
34+
$ htpasswd -c ~/.sourcegraph/config/.htpasswd langserveruser
35+
New password:
36+
Re-type new password:
37+
Adding password for user langserveruser
38+
```
39+
40+
Add a location directive the [nginx.conf](https://docs.sourcegraph.com/admin/nginx) that will route requests to the Python language server:
41+
42+
```nginx
43+
...
44+
http {
45+
...
46+
server {
47+
...
48+
location / {
49+
...
50+
}
51+
52+
location /python {
53+
proxy_pass http://host.docker.internal:4288;
54+
proxy_http_version 1.1;
55+
proxy_set_header Upgrade $http_upgrade;
56+
proxy_set_header Connection "Upgrade";
57+
58+
auth_basic "basic authentication is required to access the language server";
59+
auth_basic_user_file /etc/sourcegraph/.htpasswd;
60+
}
61+
}
62+
}
63+
```
64+
65+
- If you're running the quickstart on Linux, change `host.docker.internal` to the output of `ip addr show docker0 | grep -Po 'inet \K[\d.]+'`.
66+
- If you're using [Kubernetes](#using-kubernetes) (e.g. [deploy-sourcegraph](https://github.com/sourcegraph/deploy-sourcegraph)), change `host.docker.internal` to `lang-python`.
67+
68+
Add these to your Sourcegraph global settings:
69+
70+
```
71+
"python.serverUrl": "ws://langserveruser:PASSWORD@host.docker.internal:7080/python",
72+
"python.sourcegraphUrl": "http://host.docker.internal:7080",
73+
```
74+
75+
Fill in the `PASSWORD` that you created above.
76+
77+
- If you're running the quickstart on Linux, change `host.docker.internal` to the output of `ip addr show docker0 | grep -Po 'inet \K[\d.]+'`.
78+
- If you're using [Kubernetes](#using-kubernetes) (e.g. [deploy-sourcegraph](https://github.com/sourcegraph/deploy-sourcegraph)):
79+
- `python.serverUrl` is the address of the Python language server from the perspective of a user's browser (e.g. https://sourcegraph.example.com/python)
80+
- `python.sourcegraphUrl` is the address of the Sourcegraph instance from the perspective of the Python language server (e.g. http://sourcegraph-frontend:30080)
81+
82+
Finally, restart the sourcegraph/server container (or nginx deployment if deployed to Kubernetes) to pick up the configuration change.
83+
84+
After deploying the language server, unauthenticated access to `http://localhost:7080/python` (or https://sourcegraph.example.com/python) should be blocked, but code intelligence should work when you're logged in.
85+
86+
You can always revoke the `PASSWORD` by deleting the `.htpasswd` file and restarting nginx.
3087

3188
### Using Docker
3289

0 commit comments

Comments
 (0)