Skip to content

Commit

Permalink
Fixes for review
Browse files Browse the repository at this point in the history
  • Loading branch information
pkukielka committed Jan 15, 2025
1 parent 0f51f3a commit 0eb8453
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

import com.google.gson.annotations.SerializedName;

data class CodyContextFilterItem(
val repoNamePattern: String,
val repoNamePattern: RepoNamePatternEnum, // Oneof: .*
val filePathPatterns: List<String>? = null,
)
) {

enum class RepoNamePatternEnum {
@SerializedName(".*") Wildcard,
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package com.sourcegraph.cody.agent.protocol_generated;

object Constants {
const val Wildcard = ".*"
const val Applied = "Applied"
const val Applying = "Applying"
const val Automatic = "Automatic"
Expand All @@ -15,6 +16,7 @@ object Constants {
const val agentic = "agentic"
const val ask = "ask"
const val assistant = "assistant"
const val `auth-config-error` = "auth-config-error"
const val authenticated = "authenticated"
const val autocomplete = "autocomplete"
const val balanced = "balanced"
Expand Down
Empty file modified agent/scripts/reverse-proxy.py
100644 → 100755
Empty file.
21 changes: 21 additions & 0 deletions agent/scripts/simple-external-auth-provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python3
import json
import time
from datetime import datetime

def generate_credentials():
current_epoch = int(time.time()) + 100

credentials = {
"headers": {
"Authorization": "Bearer SomeUser",
"Expiration": current_epoch,
},
"expiration": current_epoch
}

# Print JSON to stdout
print(json.dumps(credentials))

if __name__ == "__main__":
generate_credentials()
2 changes: 1 addition & 1 deletion agent/src/cli/scip-codegen/emitters/KotlinEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class KotlinFormatter extends Formatter {
}

override formatFieldName(name: string): string {
const escaped = name.replace(':', '_').replace('/', '_')
const escaped = name.replace('.*', 'Wildcard').replace(':', '_').replace('/', '_')
const isKeyword = this.options.reserved.has(escaped)
const needsBacktick = isKeyword || !/^[a-zA-Z0-9_]+$/.test(escaped)
// Replace all non-alphanumeric characters with underscores
Expand Down
18 changes: 10 additions & 8 deletions lib/shared/src/configuration/auth-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ describe('auth-resolver', () => {
})

test('resolve custom auth provider', async () => {
const futureExpirationEpoch = 1000 * 1000 * 1000 * 1000
const futureEpoch = Date.UTC(2050) / 1000
const credentialsJson = JSON.stringify({
headers: { Authorization: 'token X' },
expiration: futureExpirationEpoch,
expiration: futureEpoch,
})

const auth = await resolveAuth(
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('auth-resolver', () => {
expect(auth.serverEndpoint).toBe('https://my-server.com/')

const headerCredential = auth.credentials as HeaderCredential
expect(headerCredential.expiration).toBe(futureExpirationEpoch)
expect(headerCredential.expiration).toBe(futureEpoch)
expect(headerCredential.getHeaders()).toStrictEqual({
Authorization: 'token X',
})
Expand All @@ -108,7 +108,7 @@ describe('auth-resolver', () => {
{
endpoint: 'https://my-server.com',
executable: {
commandLine: ['echo '],
commandLine: ['echo x'],
shell: isWindows() ? process.env.ComSpec : '/bin/bash',
timeout: 5000,
windowsHide: true,
Expand All @@ -124,14 +124,14 @@ describe('auth-resolver', () => {
expect(auth.serverEndpoint).toBe('https://my-server.com/')

expect(auth.credentials).toBe(undefined)
expect(auth.error.message).toContain('Unexpected end of JSON input')
expect(auth.error.message).toContain('Failed to execute external auth command: Unexpected token')
})

test('resolve custom auth provider error handling - bad expiration', async () => {
const pastExpirationEpoch = 1000 * 1000 * 1000
const expiredEpoch = Date.UTC(2020) / 1000
const credentialsJson = JSON.stringify({
headers: { Authorization: 'token X' },
expiration: pastExpirationEpoch,
expiration: expiredEpoch,
})

const auth = await resolveAuth(
Expand Down Expand Up @@ -159,6 +159,8 @@ describe('auth-resolver', () => {
expect(auth.serverEndpoint).toBe('https://my-server.com/')

expect(auth.credentials).toBe(undefined)
expect(auth.error.message).toContain('Credentials expiration cannot be se to the past date')
expect(auth.error.message).toContain(
'Credentials expiration cannot be set to a date in the past'
)
})
})
5 changes: 2 additions & 3 deletions lib/shared/src/configuration/auth-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ export async function resolveAuth(
if (credentials) {
if (credentials?.expiration) {
const expirationMs = credentials?.expiration * 1000
const expireInMs = expirationMs - Date.now()
if (expireInMs < 0) {
if (expirationMs < Date.now()) {
throw new Error(
'Credentials expiration cannot be se to the past date: ' +
'Credentials expiration cannot be set to a date in the past: ' +
`${new Date(expirationMs)} (${credentials.expiration})`
)
}
Expand Down
3 changes: 1 addition & 2 deletions lib/shared/src/configuration/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ async function resolveConfiguration({
const auth = await resolveAuth(serverEndpoint, clientConfiguration, clientSecrets)
const cred = auth.credentials
if (cred !== undefined && 'expiration' in cred && cred.expiration !== undefined) {
const expirationMs = cred.expiration * 1000
const expireInMs = expirationMs - Date.now()
const expireInMs = cred.expiration * 1000 - Date.now()
setInterval(() => _refreshConfigRequests.next(), expireInMs)
}
return { configuration: clientConfiguration, clientState, auth, isReinstall }
Expand Down

0 comments on commit 0eb8453

Please sign in to comment.