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

fix: multiple issues with gofalcon example and client opts #38

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 19 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,28 @@ func (c config) OK() error {

1. `config`: A type the raw json config is unmarshalled into.
2. `logger`: A dedicated logger is provided to capture function logs in all environments (both locally and distributed).
1. Using a different logger may produce logs in the runtime but won't make it into the logscale infrastructure.
1. Using a different logger may produce logs in the runtime but won't make it into the logscale infrastructure.
3. `Request`: Request payload and metadata. At the time of this writing, the `Request` struct consists of:
1. `Body`: The input io.Reader for the payload as given in the Function Gateway `body` payload field or streamed
in.
2. `Params`: Contains request headers and query parameters.
3. `URL`: The request path relative to the function as a string.
4. `Method`: The request HTTP method or verb.
5. `Context`: Caller-supplied raw context.
6. `AccessToken`: Caller-supplied access token.
1. `Body`: The input io.Reader for the payload as given in the Function Gateway `body` payload field or streamed
in.
2. `Params`: Contains request headers and query parameters.
3. `URL`: The request path relative to the function as a string.
4. `Method`: The request HTTP method or verb.
5. `Context`: Caller-supplied raw context.
6. `AccessToken`: Caller-supplied access token.
4. `RequestOf`: The same as Request only that the Body field is json unmarshalled into the generic type (i.e. `request`
type above)
5. `Response`
1. The `Response` contains fields `Body` (the payload of the response), `Code` (an HTTP status code),
`Errors` (a slice of `APIError`s), and `Headers` (a map of any special HTTP headers which should be present on
the response).
1. The `Response` contains fields `Body` (the payload of the response), `Code` (an HTTP status code),
`Errors` (a slice of `APIError`s), and `Headers` (a map of any special HTTP headers which should be present on
the response).
6. `main()`: Initialization and bootstrap logic all contained with fdk.Run and handler constructor.

more examples can be found at:

* [fn with config](examples/fn_config)
* [fn without config](examples/fn_no_config)
* [more complex/complete example](examples/complex)
- [fn with config](examples/fn_config)
- [fn without config](examples/fn_no_config)
- [more complex/complete example](examples/complex)

### Testing locally

Expand Down Expand Up @@ -161,23 +161,19 @@ import (
"log/slog"

fdk "github.com/CrowdStrike/foundry-fn-go"
"github.com/CrowdStrike/gofalcon/falcon"
"github.com/CrowdStrike/gofalcon/falcon/client"
"github.com/crowdstrike/gofalcon/falcon"
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is very interesting, why does it need to be lowercase for gofalcon but not foundry-fn-go? Its the same github org yah?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The difference is from the go.mod - it's lowercase in gofalcon but capital CS in foundry-fn-go.

Copy link
Collaborator

Choose a reason for hiding this comment

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

super weird, curious why that's the case 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIK all lower is the convention

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know we've had users say they have to use the mixed case for go get, or it fails, which is super annoying. Ideally it'd be all lowercase. I'll have to figure out what's going on there.

"github.com/crowdstrike/gofalcon/falcon/client"
)

func newHandler(_ context.Context, _ *slog.Logger, cfg config) fdk.Handler {
mux := fdk.NewMux()
mux.Post("/echo", fdk.HandlerFn(func(ctx context.Context, r fdk.Request) fdk.Response {
client, err := newFalconClient(ctx, r.AccessToken)
if err != nil {
if err == falcon.ErrFalconNoToken {
// not a processable request
return fdk.Response{ /* snip */ }
}
// some other error - see gofalcon documentation
return fdk.Response{ Code: 500, Body: fdk.JSON(err) }
Copy link
Collaborator

@jsteenb2 jsteenb2 Dec 30, 2024

Choose a reason for hiding this comment

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

What's the motivation for returning a jsonified err? I think we're better off sticking with the fdk.ErrResp(fdk.APIError{Code: 500, Message: err.Error()}. This ties into fusion's execution log UI as well as align with the idiomatic error handling of errors in a funciton.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ack hadn't seen this, will update.

Copy link
Collaborator

@jsteenb2 jsteenb2 Dec 30, 2024

Choose a reason for hiding this comment

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

unfortunately, looking at the sample apps for foundry, we don't have funcitons that utilize even a small percentage of our best practices. They need some TLC, so things like fdk.ErrResp, fdk.HandleFnOf, logging, etc are in plain view

}

// trim rest
// we have a valid gofalcon client
}))
return mux
}
Expand All @@ -188,7 +184,7 @@ func newFalconClient(ctx context.Context, token string) (*client.CrowdStrikeAPIS
AccessToken: token,
Cloud: falcon.Cloud(opts.Cloud),
Context: ctx,
UserAgentOverride: out.UserAgent,
UserAgentOverride: opts.UserAgent,
})
}

Expand Down
3 changes: 1 addition & 2 deletions falcon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ var version = "development"
// AccessToken: token,
// Cloud: falcon.Cloud(opts.Cloud),
// Context: ctx,
// UserAgentOverride: out.UserAgent,
// UserAgentOverride: opts.UserAgent,
Copy link
Collaborator

Choose a reason for hiding this comment

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

oof good catch 👍

// })
// }
func FalconClientOpts() (out struct {
Cloud string
UserAgent string
}) {
c := strings.ToLower(os.Getenv("CS_CLOUD"))
c = strings.ReplaceAll(c, "-", "")
c = strings.TrimSpace(c)
if c == "" {
c = "us-1"
Expand Down
Loading