Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to force usage of pre-defined endpoint #6574

Merged
merged 1 commit into from
Jan 10, 2025

Conversation

pkukielka
Copy link
Contributor

@pkukielka pkukielka commented Jan 9, 2025

Fixes https://linear.app/sourcegraph/issue/CODY-4644/

Can be used by enterprise clients which want to prevent users from logging into wrong endpoints.

Changes

  1. If cody.override.serverEndpoint is defined in the config does not allow to login using other endpoint, and automatically show only enterprise login screen:

image

  1. If user is already logged in do not allow to switch account. I intentionally allowed to sign-out (which will redirect user to screen 1.) as it might be useful e.g. to force token refresh in case to problems.

image

Test plan

  1. Open VSC and add this entry to the settings.json:
    "cody.override.serverEndpoint": "https://sg02.sourcegraphcloud.com/"
  2. You should get logged out and moved to the login screen from the screenshot 1.
  3. Endpoint should not be editable, the only possible aciton should be clicking Sign In button
  4. Proceed with singin, you should sign-in successfully
  5. Click on the user menu (account panel) - there should be no Switch Account option present (as on screenshot 2).
  6. Sign Out
  7. You should be moved to the login screen from the screenshot 1.

Changelog

@pkukielka pkukielka force-pushed the pkukielka/make-overrideServerEndpoint-configurable branch from 828ed55 to 159ab90 Compare January 9, 2025 14:45
@pkukielka pkukielka force-pushed the pkukielka/make-overrideServerEndpoint-configurable branch from 159ab90 to cb8addf Compare January 9, 2025 14:45
@@ -155,7 +155,7 @@ export const TabsBar = memo<TabsBarProps>(props => {
authStatus={user.user as AuthenticatedAuthStatus}
isProUser={isCodyProUser}
endpointHistory={endpointHistory}
setView={setView}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used anymore.

@pkukielka pkukielka merged commit 04ef6cf into main Jan 10, 2025
21 checks passed
@pkukielka pkukielka deleted the pkukielka/make-overrideServerEndpoint-configurable branch January 10, 2025 11:31
pkukielka added a commit that referenced this pull request Jan 14, 2025
Fixes https://linear.app/sourcegraph/issue/CODY-4642

## External Authentication Provider Support for Cody

This PR introduces support for external authentication providers in
Cody, allowing users to integrate with custom authentication proxies and
handle complex authentication scenarios.

### Feature Overview

This feature requires clients to have reverse proxy and custom
sourcegraph instance [configured to use HTTP
authentication](https://sourcegraph.com/docs/admin/auth#http-authentication-proxies).

The external authentication provider feature allows clients to generate,
for a specified endpoint, custom auth headers. Those headers will be
attached to every authenticated http request instead of the normal
`"Authorization": "token sgp_SOME_TOKEN"` auth header.

To generate those custom headers client need to specify command that
generates authentication headers for specific endpoints. The command
must output a JSON object containing header key-value pairs on stdout.
Those endpoints URLs needs to point to [proxies configured by
client](https://sourcegraph.com/docs/admin/auth#http-authentication-proxies)
which redirects requests to the custom sourcegraph instance.

Whole flow looks like this:

1. When Cody attempts to connect to a endpoint which has defined
external provider it executes the specified command
2. The command outputs a JSON object containing header key-value pairs
on stdout
3. These headers are attached to subsequent authorised requests to the
endpoint
4. The proxy server processes these headers and converts them to
appropriate `X-Forwarded-User` and/or `X-Forwarded-Email` headers as
specified in the
[documentation](https://sourcegraph.com/docs/admin/auth#http-authentication-proxies)
5. The Sourcegraph instance authenticates the user based on these
forwarded headers

### Configuration

Users can configure custom authentication providers in their vscode
settings.json using the following structure:

```json
"cody.auth.externalProviders": [
    {
        "endpoint": "http://localhost:5555",
        "executable": {
            "commandLine": ["echo '{ \"headers\": { \"Authorization\": \"Bearer SomeUser\" } }'"],
            "shell": "/bin/bash",       // Optional: Shell to execute the command with. Default: '/bin/sh' on Unix, process.env.ComSpec on Windows.
            "environment": {            // Optional: Additional environment variables
                "SOME_ENV": "VALUE"
            },
            "timeout": 5000,            // Optional: Timeout in milliseconds
            "windowsHide": true         // Optional: Hide the window on Windows
        }
    }
]
```

It can also be configured in IntelliJ using settings editor:

![image](https://github.com/user-attachments/assets/5440b226-534f-471c-a78e-3c6f6d9c76c0)

User can define as many external providers as needed.

If only one provider is needed and login using this provider should be
forced, it [will be possible to
accomplish](#6574) using
`overrideServerEndpoint`.

### Configuration Options
* endpoint: The URL of the proxy server that will handle the
authentication
* executable: Configuration for the command that generates
authentication headers
  - commandLine: Array of command and arguments to execute
  - shell: (Optional) Specific shell to use for command execution
- environment: (Optional) Additional environment variables for the
command
  - workingDir: (Optional) Working directory for command execution
  - timeout: (Optional) Command execution timeout
  - windowsHide: (Optional) Hide window when executing on Windows

### Expected Output

Script or executable specified in the configuration have to return valid
JSON object which adheres to the schema:

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["headers"],
  "properties": {
    "headers": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    },
    "expiration": {
      "type": "number"
    }
  }
}
```

Where:

* `headers` *(Required)* [Map with string keys and values] - headers
which will be attached to authenticated requests to the http proxy
* `expiration` *(Optional)* [a number] - Epoch Unix Timestamp (UTC) of
the headers expiration date; after expiration date headers will be
re-generated automatically using configured command
 
### Testing Locally

1. Start the provided reverse proxy:
`python agent/scripts/reverse-proxy.py
https://your-sourcegraph-instance.com 5555`
You can choose different port or start a few different proxies for
different endpoints.

2. Add the proxy configuration to your settings:

```json
"cody.auth.externalProviders": [
    {
        "endpoint": "http://localhost:5555",
        "executable": {
            "commandLine": ["echo '{ \"Authorization\": \"Bearer TestUser\" }'"],
            "shell": "/bin/bash"
        }
    }
]
```

3. In Cody sign in to `http://localhost:5555` endpoint
4. Verify that you're authenticated as TestUser.

### Security Considerations
1. Ensure that the proxy server properly validates and sanitizes
authentication headers
2. The executable should be secured and have appropriate permissions
3. Consider using HTTPS for the proxy endpoint in production
environments

### Missing features
1. Fastpath users custom tokens for authentication, we need to check if
and how we can support it with custom auth providers.
2. Cli is currently not supported, but should be trivial to add support
for it.

## Test plan

1. Setup local testing environment as described in the `Testing Locally`
section.
3. Run a full QA.

## Changelog

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants