Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions plugin/gthulhu/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ type JWTClient struct {
token string
tokenExpiresAt time.Time
httpClient *http.Client
authEnabled bool
}

// NewJWTClient creates a new JWT client
func NewJWTClient(publicKeyPath, apiBaseURL string) *JWTClient {
func NewJWTClient(
publicKeyPath,
apiBaseURL string,
authEnabled bool,
) *JWTClient {
return &JWTClient{
publicKeyPath: publicKeyPath,
apiBaseURL: strings.TrimSuffix(apiBaseURL, "/"),
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
authEnabled: authEnabled,
}
}
Comment on lines +46 to 59
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

The GetAuthenticatedClient method does not respect the authEnabled flag. While MakeAuthenticatedRequest was updated to conditionally add authentication based on authEnabled, GetAuthenticatedClient still always calls ensureValidToken and creates an authenticatedTransport that adds the Authorization header. This means any code using GetAuthenticatedClient will still attempt JWT authentication even when authEnabled is false. The method should check the authEnabled flag and either return a plain HTTP client or skip token validation when authentication is disabled.

Copilot uses AI. Check for mistakes.

Expand Down Expand Up @@ -164,17 +170,18 @@ func (c *JWTClient) GetAuthenticatedClient() (*http.Client, error) {

// MakeAuthenticatedRequest makes an HTTP request with JWT authentication
func (c *JWTClient) MakeAuthenticatedRequest(method, url string, body io.Reader) (*http.Response, error) {
if err := c.ensureValidToken(); err != nil {
return nil, err
}

req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

// Add Authorization header
req.Header.Set("Authorization", "Bearer "+c.token)
if c.authEnabled {
if err := c.ensureValidToken(); err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
}
req.Header.Set("Content-Type", "application/json")

return c.httpClient.Do(req)
Expand Down
15 changes: 11 additions & 4 deletions plugin/gthulhu/gthulhu.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ func init() {
// Initialize JWT client if API config is provided
if config.APIConfig.Enabled &&
config.APIConfig.PublicKeyPath != "" && config.APIConfig.BaseURL != "" {
err := gthulhuPlugin.InitJWTClient(config.APIConfig.PublicKeyPath, config.APIConfig.BaseURL)
err := gthulhuPlugin.InitJWTClient(
config.APIConfig.PublicKeyPath,
config.APIConfig.BaseURL,
config.APIConfig.AuthEnabled,
)
Comment on lines 30 to +36
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

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

When authEnabled is false, the JWT client initialization still requires valid PublicKeyPath and BaseURL values (checked at lines 30-31). However, when authentication is disabled, the public key path is unnecessary since no JWT token will be requested. Consider either relaxing this validation when authEnabled is false, or updating the initialization condition to check authEnabled alongside the other parameters.

Copilot uses AI. Check for mistakes.
if err != nil {
return nil, err
}

// Initialize metrics client
err = gthulhuPlugin.InitMetricsClient(config.APIConfig.BaseURL)
if err != nil {
Expand Down Expand Up @@ -301,8 +304,12 @@ func (g *GthulhuPlugin) getTaskExecutionTime(pid int32) uint64 {
}

// InitJWTClient initializes the JWT client for API authentication
func (g *GthulhuPlugin) InitJWTClient(publicKeyPath, apiBaseURL string) error {
g.jwtClient = NewJWTClient(publicKeyPath, apiBaseURL)
func (g *GthulhuPlugin) InitJWTClient(
publicKeyPath,
apiBaseURL string,
authEnabled bool,
) error {
g.jwtClient = NewJWTClient(publicKeyPath, apiBaseURL, authEnabled)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions plugin/internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type APIConfig struct {
BaseURL string `yaml:"base_url"`
Interval int `yaml:"interval"`
Enabled bool `yaml:"enabled"`
AuthEnabled bool `yaml:"auth_enabled"`
}

// SchedConfig holds the configuration parameters for creating a scheduler plugin
Expand Down
Loading