From a46a591efca8ca86e77e0a1136d3e0347c0acd4b Mon Sep 17 00:00:00 2001 From: Keepers Date: Fri, 17 Mar 2023 17:48:13 -0600 Subject: [PATCH] add 10 day deadline to cli context (#2852) Ensures all CLI usage has a context deadline so that we don't bump into the graph api 100 second deadline. --- - [x] :white_check_mark: Yes, it's included - [x] :bug: Bugfix - [x] :muscle: Manual --- CHANGELOG.md | 5 ++++ src/cli/cli.go | 7 +++++- src/internal/connector/graph/service.go | 24 +++++++++++--------- src/internal/connector/graph/service_test.go | 3 +-- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c1be35a..0f9ce24d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix repo connect not working without a config file - Fix item re-download on expired links silently being skipped +- Improved permissions backup and restore for OneDrive +- CLI calls default to a 10-day context deadline to avoid letting graph api restrict requests to a 100 second deadline. + +### Known Issues +- Owner (Full control) or empty (Restricted View) roles cannot be restored for OneDrive ## [v0.5.0] (beta) - 2023-03-13 diff --git a/src/cli/cli.go b/src/cli/cli.go index 051e859f6..8b8cf3b57 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -5,6 +5,7 @@ import ( "os" "regexp" "strings" + "time" "github.com/spf13/cobra" "golang.org/x/exp/slices" @@ -146,8 +147,12 @@ func BuildCommandTree(cmd *cobra.Command) { // Handle builds and executes the cli processor. func Handle() { + tenDays := time.Now().Add(time.Hour * 24 * 10) //nolint:forbidigo - ctx := config.Seed(context.Background()) + ctx, cancel := context.WithDeadline(context.Background(), tenDays) + defer cancel() + + ctx = config.Seed(ctx) ctx = print.SetRootCmd(ctx, corsoCmd) observe.SeedWriter(ctx, print.StderrWriter(ctx), observe.PreloadFlags()) diff --git a/src/internal/connector/graph/service.go b/src/internal/connector/graph/service.go index 5eb831a8a..03ffdfa4f 100644 --- a/src/internal/connector/graph/service.go +++ b/src/internal/connector/graph/service.go @@ -22,16 +22,18 @@ import ( ) const ( - logGraphRequestsEnvKey = "LOG_GRAPH_REQUESTS" - numberOfRetries = 3 - retryAttemptHeader = "Retry-Attempt" - retryAfterHeader = "Retry-After" - defaultMaxRetries = 3 - defaultDelay = 3 * time.Second - absoluteMaxDelaySeconds = 180 - rateLimitHeader = "RateLimit-Limit" - rateRemainingHeader = "RateLimit-Remaining" - rateResetHeader = "RateLimit-Reset" + logGraphRequestsEnvKey = "LOG_GRAPH_REQUESTS" + log2xxGraphRequestsEnvKey = "LOG_2XX_GRAPH_REQUESTS" + numberOfRetries = 3 + retryAttemptHeader = "Retry-Attempt" + retryAfterHeader = "Retry-After" + defaultMaxRetries = 3 + defaultDelay = 3 * time.Second + absoluteMaxDelaySeconds = 180 + rateLimitHeader = "RateLimit-Limit" + rateRemainingHeader = "RateLimit-Remaining" + rateResetHeader = "RateLimit-Reset" + defaultHTTPClientTimeout = 1 * time.Hour ) // AllMetadataFileNames produces the standard set of filenames used to store graph @@ -195,7 +197,7 @@ func HTTPClient(opts ...option) *http.Client { noOfRetries, minRetryDelay := clientconfig.applyMiddlewareConfig() middlewares := GetKiotaMiddlewares(&clientOptions, noOfRetries, minRetryDelay) httpClient := msgraphgocore.GetDefaultClient(&clientOptions, middlewares...) - httpClient.Timeout = time.Minute * 3 + httpClient.Timeout = defaultHTTPClientTimeout clientconfig.apply(httpClient) diff --git a/src/internal/connector/graph/service_test.go b/src/internal/connector/graph/service_test.go index bf7241154..d88e92474 100644 --- a/src/internal/connector/graph/service_test.go +++ b/src/internal/connector/graph/service_test.go @@ -3,7 +3,6 @@ package graph import ( "net/http" "testing" - "time" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/stretchr/testify/assert" @@ -54,7 +53,7 @@ func (suite *GraphUnitSuite) TestHTTPClient() { name: "no options", opts: []option{}, check: func(t *testing.T, c *http.Client) { - assert.Equal(t, 3*time.Minute, c.Timeout, "default timeout") + assert.Equal(t, defaultHTTPClientTimeout, c.Timeout, "default timeout") }, }, {