-
Notifications
You must be signed in to change notification settings - Fork 2.1k
InstallScripts: pin teleport version using ServerVersion #28149
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
Merged
marcoandredinis
merged 2 commits into
master
from
marco/auto_upgrade_script_fix_stablecloud
Jun 23, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| Copyright 2023 Gravitational, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package automaticupgrades | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/gravitational/trace" | ||
|
|
||
| "github.com/gravitational/teleport" | ||
| "github.com/gravitational/teleport/lib/utils" | ||
| ) | ||
|
|
||
| const ( | ||
| // stableCloudVersionBaseURL is the base URL for the server that returns the current stable/cloud version. | ||
| stableCloudVersionBaseURL = "https://updates.releases.teleport.dev" | ||
|
|
||
| // stableCloudVersionPath is the URL path that returns the current stable/cloud version. | ||
| stableCloudVersionPath = "/v1/stable/cloud/version" | ||
| ) | ||
|
|
||
| // Version returns the version that should be used for installing Teleport Services | ||
| // This is used when installing agents using scripts. | ||
| // Even when Teleport Auth/Proxy is using vX, the agents must always respect this version. | ||
| func Version(ctx context.Context, baseURL string) (string, error) { | ||
| if baseURL == "" { | ||
| baseURL = stableCloudVersionBaseURL | ||
| } | ||
|
|
||
| fullURL, err := url.JoinPath(baseURL, stableCloudVersionPath) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := utils.ReadAtMost(resp.Body, teleport.MaxHTTPResponseSize) | ||
| if err != nil { | ||
| return "", trace.Wrap(err) | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", trace.BadParameter("invalid status code %d, body: %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| versionString := strings.TrimSpace(string(body)) | ||
|
|
||
| return versionString, trace.Wrap(err) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| Copyright 2023 Gravitational, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package automaticupgrades | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestVersion(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| isBadParameterErr := func(tt require.TestingT, err error, i ...interface{}) { | ||
| require.True(tt, trace.IsBadParameter(err), "expected bad parameter, got %v", err) | ||
| } | ||
|
|
||
| for _, tt := range []struct { | ||
| name string | ||
| mockStatusCode int | ||
| mockResponseString string | ||
| errCheck require.ErrorAssertionFunc | ||
| expectedVersion string | ||
| }{ | ||
| { | ||
| name: "real response", | ||
| mockStatusCode: http.StatusOK, | ||
| mockResponseString: "v13.1.1\n", | ||
| errCheck: require.NoError, | ||
| expectedVersion: "v13.1.1", | ||
| }, | ||
| { | ||
| name: "invalid status code (500)", | ||
| mockStatusCode: http.StatusInternalServerError, | ||
| errCheck: isBadParameterErr, | ||
| }, | ||
| { | ||
| name: "invalid status code (403)", | ||
| mockStatusCode: http.StatusForbidden, | ||
| errCheck: isBadParameterErr, | ||
| }, | ||
| { | ||
| name: "valid but has spaces", | ||
| mockStatusCode: http.StatusOK, | ||
| mockResponseString: " v13.1.1 \n \r\n", | ||
| errCheck: require.NoError, | ||
| expectedVersion: "v13.1.1", | ||
| }, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| httpTestServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| assert.Equal(t, r.URL.Path, "/v1/stable/cloud/version") | ||
| w.WriteHeader(tt.mockStatusCode) | ||
| w.Write([]byte(tt.mockResponseString)) | ||
| })) | ||
| defer httpTestServer.Close() | ||
|
|
||
| v, err := Version(ctx, httpTestServer.URL) | ||
| tt.errCheck(t, err) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| require.Equal(t, v, tt.expectedVersion) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems irrelevant to the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was causing an import cycle because:
It was only used in our binaries and it was a one liner, so I just replaced the function call