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

[GitHub] Add a GraphQL client to the connector #3837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rgmz
Copy link
Contributor

@rgmz rgmz commented Jan 11, 2025

Description:

This introduces a latent GraphQL client without making any functional changes. The GraphQL API is required for certain features (e.g., #1906), and it is already being manually called in at least once place:

req, err := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(requestBody))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

shurcooL/githubv4 is the library recommended by the authors of google/go-github.

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@rgmz rgmz requested review from a team as code owners January 11, 2025 15:10
@rgmz rgmz marked this pull request as draft January 11, 2025 15:11
@rgmz rgmz force-pushed the feat/github-graphql branch from b3fcb92 to c2bc286 Compare January 11, 2025 15:34
@rgmz rgmz marked this pull request as ready for review January 11, 2025 15:36
@rgmz rgmz force-pushed the feat/github-graphql branch 3 times, most recently from 37ec6bc to c067504 Compare January 11, 2025 18:38
Copy link
Collaborator

@rosecodym rosecodym left a comment

Choose a reason for hiding this comment

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

Pretty cool! It looks like these clients are all unauthenticated - out of curiosity, do you foresee them ever being authenticated?

default:
return nil, fmt.Errorf("unknown connection type")
}
}

func createAPIClient(ctx context.Context, httpClient *http.Client, apiEndpoint string) (*github.Client, error) {
ctx.Logger().WithName("github").V(2).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this logger name be added way higher than the call stack, at entry to the overall github code? I presume you didn't do that because github.com/trufflesecurity/trufflehog/v3/pkg/context doesn't support WithName, but I don't think kludging around that by adding contextual information in the "wrong" spot like this is a good solution overall because we've seen it contribute to bifurcation of our loggers (where a chunk of code is using two loggers at once, each with different contextual information), which in turn has caused concrete debugging problems.

To be clear, I'm not worried that this specific code change will cause an issue - I'm worried that in nine months somebody else is going to copy the pattern somewhere else without thinking too hard about it and create a maintainability problem. If you want to add this piece of context, I think it's safer in the long term to add it at the entry to the github code either using WithValue or with a new WithName that you add to our logging package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the current implementation doesn't make it easy to have a named logger for a package. You either have to call .WithName() repeatedly or pass a logger separately -- neither are ideal.

I think it's safer in the long term to add ... a new WithName that you add to our logging package.

I can try to tackle that in a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

WithName functionality can't be added to the logger Context. Calls to WithName are appended to the logger, meaning that a context passed between packages could become trufflehog.github.git.detector.etc.

The best approach is probably a simple helper function in the package.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if this is what you're going for, but a logger in a context can be modified via:

ctx = context.WithLogger(ctx, ctx.Logger().WithName("github"))

Not the most ergonomic, but imo not completely foreign (might be just me since I wrote trufflehog/pkg/context and trufflehog/pkg/log though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if this is what you're going for, but a logger in a context can be modified via:

ctx = context.WithLogger(ctx, ctx.Logger().WithName("github"))

Not the most ergonomic, but imo not completely foreign (might be just me since I wrote trufflehog/pkg/context and trufflehog/pkg/log though).

I think this would still have the issue of 'unbounded' logger names:

Calls to WithName are appended to the logger, meaning that a context passed between packages could become trufflehog.github.git.detector.etc.

There's no way to unset or overwrite the logger name that I'm aware of.

@rgmz
Copy link
Contributor Author

rgmz commented Jan 14, 2025

Pretty cool! It looks like these clients are all unauthenticated - out of curiosity, do you foresee them ever being authenticated?

It should inherit authentication from the HTTP client's transport:

httpClient := common.RetryableHTTPClientTimeout(int64(httpTimeoutSeconds))
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient.Transport = &oauth2.Transport{
Base: httpClient.Transport,
Source: tokenSource,
}

@rosecodym
Copy link
Collaborator

🤦 no idea how i missed that

@rgmz rgmz force-pushed the feat/github-graphql branch 3 times, most recently from d0bd942 to 6c7f34e Compare January 20, 2025 14:55
@rgmz rgmz force-pushed the feat/github-graphql branch 2 times, most recently from e1384ed to ddf8805 Compare January 29, 2025 02:22
@rgmz rgmz force-pushed the feat/github-graphql branch from ddf8805 to b3ede0c Compare February 2, 2025 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants