-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add OS keyring support for session tokens #307
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
base: main
Are you sure you want to change the base?
Changes from all commits
0bc801c
a5488c8
bba2fbc
ad43762
c9008c0
6d264b1
be793d9
d164f81
a715f68
6cd4e6d
7ae1d31
5d5d21e
1616c37
cf073b1
1e9d6ca
7f86439
f9b872b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,17 +17,20 @@ import com.coder.toolbox.sdk.v2.models.Workspace | |
| import com.coder.toolbox.sdk.v2.models.WorkspaceAgent | ||
| import com.coder.toolbox.settings.SignatureFallbackStrategy.ALLOW | ||
| import com.coder.toolbox.util.InvalidVersionException | ||
| import com.coder.toolbox.util.OS | ||
| import com.coder.toolbox.util.SemVer | ||
| import com.coder.toolbox.util.escape | ||
| import com.coder.toolbox.util.escapeSubcommand | ||
| import com.coder.toolbox.util.getOS | ||
| import com.coder.toolbox.util.runProcess | ||
| import com.coder.toolbox.util.safeHost | ||
| import com.coder.toolbox.util.sanitizeSecrets | ||
| import com.squareup.moshi.Json | ||
| import com.squareup.moshi.JsonClass | ||
| import com.squareup.moshi.JsonDataException | ||
| import com.squareup.moshi.Moshi | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import org.zeroturnaround.exec.ProcessExecutor | ||
| import retrofit2.Retrofit | ||
| import java.io.EOFException | ||
| import java.io.FileNotFoundException | ||
|
|
@@ -104,6 +107,7 @@ data class Features( | |
| val reportWorkspaceUsage: Boolean = false, | ||
| val wildcardSsh: Boolean = false, | ||
| val buildReason: Boolean = false, | ||
| val keyringAuth: Boolean = false, | ||
| ) | ||
|
|
||
| /** | ||
|
|
@@ -112,7 +116,8 @@ data class Features( | |
| class CoderCLIManager( | ||
| private val context: CoderToolboxContext, | ||
| // The URL of the deployment this CLI is for. | ||
| private val deploymentURL: URL | ||
| private val deploymentURL: URL, | ||
| private val currentOs: OS? = getOS(), | ||
| ) { | ||
| private val downloader = createDownloadService() | ||
| private val gpgVerifier = GPGVerifier(context) | ||
|
|
@@ -260,17 +265,24 @@ class CoderCLIManager( | |
| } | ||
|
|
||
| /** | ||
| * Use the provided token to initializeSession the CLI. | ||
| * Use the provided token to initialize the CLI. | ||
| * | ||
| * When keyring storage is enabled and supported, omit --global-config so supported CLIs | ||
| * can select their default OS-backed credential storage. This only applies | ||
| * on macOS and Windows. | ||
| */ | ||
| fun login(token: String): String { | ||
| context.logger.info("Storing CLI credentials in $coderConfigPath") | ||
| return exec( | ||
| fun login(token: String, feats: Features = features): String { | ||
| val args = mutableListOf( | ||
| "login", | ||
| "--use-token-as-session", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we only pass this when keyring is enabled and supported to maintain the old behavior?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I've discussed with the team, I think the old behavior is somewhat wrong. Basically the user generated an API token via /cli-auth, which is then used by the REST API client for polling on workspaces and other stuffy and also for the CLI login. However, in the old approach the CLI was using the passed api token to mint a new token that is used for workspace start and ssh. There is mismatch in the token passed by the user what the CLI is using. It would also be harder for us to implement logout the feature (which today doesn't do anything serious in Coder Toolbox). And of course the other argument is that there is a requirement (I can find the ticket) to have the plugin aligned as much as possible with the VS Code extension. |
||
| deploymentURL.toString(), | ||
| "--token", | ||
| token, | ||
| "--global-config", | ||
| coderConfigPath.toString(), | ||
| ) | ||
| if (!shouldUseKeyringAuth(feats)) { | ||
| args.addAll(globalConfigArgs()) | ||
| } | ||
| return exec( | ||
| env = mapOf(CODER_SESSION_TOKEN_ENV_VAR to token), | ||
| *args.toTypedArray(), | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -279,8 +291,7 @@ class CoderCLIManager( | |
| */ | ||
| fun startWorkspace(workspaceOwner: String, workspaceName: String, feats: Features = features): String { | ||
| val args = mutableListOf( | ||
| "--global-config", | ||
| coderConfigPath.toString(), | ||
| *workspaceAuthArgs(feats).toTypedArray(), | ||
| "start", | ||
| "--yes", | ||
| "$workspaceOwner/$workspaceName" | ||
|
|
@@ -336,8 +347,8 @@ class CoderCLIManager( | |
| val baseArgs = | ||
| listOfNotNull( | ||
| escape(localBinaryPath.toString()), | ||
| "--global-config", | ||
| escape(coderConfigPath.toString()), | ||
| if (!shouldUseKeyringAuth(feats)) "--global-config" else null, | ||
| if (!shouldUseKeyringAuth(feats)) escape(coderConfigPath.toString()) else null, | ||
| // CODER_URL might be set, and it will override the URL file in | ||
| // the config directory, so override that here to make sure we | ||
| // always use the correct URL. | ||
|
|
@@ -534,17 +545,19 @@ class CoderCLIManager( | |
| return matches | ||
| } | ||
|
|
||
| private fun exec(vararg args: String): String { | ||
| val stdout = | ||
| ProcessExecutor() | ||
| .command(localBinaryPath.toString(), *args) | ||
| .environment("CODER_HEADER_COMMAND", context.settingsStore.headerCommand) | ||
| .exitValues(0) | ||
| .readOutput(true) | ||
| .execute() | ||
| .outputUTF8() | ||
| val redactedArgs = listOf(*args).joinToString(" ").replace(tokenRegex, "--token <redacted>") | ||
| context.logger.info("`$localBinaryPath $redactedArgs`: $stdout") | ||
| private fun exec(vararg args: String): String = exec(env = emptyMap(), *args) | ||
|
|
||
| private fun exec(env: Map<String, String>, vararg args: String): String { | ||
| val command = listOf(localBinaryPath.toString(), *args) | ||
| val processEnv = buildMap { | ||
| context.settingsStore.headerCommand?.let { put("CODER_HEADER_COMMAND", it) } | ||
| putAll(env) | ||
| } | ||
|
|
||
| val stdout = runProcess(command, environment = processEnv).stdout | ||
| val sanitizedArgs = listOf(*args).joinToString(" ").sanitizeSecrets() | ||
| val sanitizedStdout = stdout.sanitizeSecrets() | ||
| context.logger.info("`$localBinaryPath $sanitizedArgs`: $sanitizedStdout") | ||
| return stdout | ||
| } | ||
|
|
||
|
|
@@ -559,6 +572,7 @@ class CoderCLIManager( | |
| reportWorkspaceUsage = version >= SemVer(2, 13, 0), | ||
| wildcardSsh = version >= SemVer(2, 19, 0), | ||
| buildReason = version >= SemVer(2, 25, 0), | ||
| keyringAuth = version >= SemVer(2, 29, 0), | ||
| ) | ||
| } | ||
| } | ||
|
|
@@ -572,7 +586,9 @@ class CoderCLIManager( | |
| } | ||
|
|
||
| companion object { | ||
| private val tokenRegex = "--token [^ ]+".toRegex() | ||
| private const val CODER_SESSION_TOKEN_ENV_VAR = "CODER_SESSION_TOKEN" | ||
|
|
||
| internal fun supportsKeyringStorage(os: OS?): Boolean = os == OS.MAC || os == OS.WINDOWS | ||
|
|
||
| private fun getHostnamePrefix(url: URL): String = "coder-jetbrains-toolbox-${url.safeHost()}" | ||
|
|
||
|
|
@@ -583,4 +599,17 @@ class CoderCLIManager( | |
|
|
||
| private fun Pair<Workspace, WorkspaceAgent>.agent() = this.second | ||
| } | ||
|
|
||
| private fun globalConfigArgs(): List<String> = listOf("--global-config", coderConfigPath.toString()) | ||
|
|
||
| private fun workspaceAuthArgs(feats: Features): List<String> = | ||
| if (shouldUseKeyringAuth(feats)) { | ||
| listOf("--url", deploymentURL.toString()) | ||
| } else { | ||
| globalConfigArgs() | ||
| } | ||
|
|
||
| private fun shouldUseKeyringAuth(feats: Features): Boolean = | ||
| context.settingsStore.useKeyring && feats.keyringAuth && supportsKeyringStorage(currentOs) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any visibility for users that enable the feature/setting, but it isn't enabled due to this logic? I am mainly thinking about how we avoid silently falling back to file storage.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On top of your recommandations I added another commit that hides the checkbox field on unsupported operating systems (Linux) |
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds the write side of keyring but not the read side. VS Code has a second feature flag
keyringTokenReadfor CLI 2.31.0+ that callscoder login token --url <url>so the plugin can pick up tokens a user wrote withcoder loginin their terminal. Worth adding here, or filing a follow-up?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would raise a new ticket for this part honestly. Sounds like a new feature, probably some code will have to be reworked. I will check with @matifali if this feature makes sense for Coder Toolbox, but good point - thank you for bringing that up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EhabY I talked with Atif and raised https://linear.app/codercom/issue/DEVEX-403/add-terminal-login-token-pickup-to-coder-toolbox