-
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 8 commits
0bc801c
a5488c8
bba2fbc
ad43762
c9008c0
6d264b1
be793d9
d164f81
a715f68
6cd4e6d
7ae1d31
5d5d21e
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 { | ||
|
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. This adds the write side of keyring but not the read side. VS Code has a second feature flag |
||
| 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? |
||
| 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,18 @@ 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 sanitizedStdout = stdout.sanitizeSecrets() | ||
| context.logger.info("`$localBinaryPath ${listOf(*args).joinToString(" ")}`: $sanitizedStdout") | ||
|
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.
|
||
| return stdout | ||
| } | ||
|
|
||
|
|
@@ -559,6 +571,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 +585,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 +598,16 @@ 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.
Reads like future work mixed into present-tense docs, a bit confusing