forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hosted plugin manager prerequisites (gravitational#23922)
* Expose Ping() in bare auth server * Handle both pointer and bare PluginStatusV1 * Add metric name * Add StatusSink * Run GCI * Move comment back to auth_with_roles * Update lib/auth/auth.go Co-authored-by: Alan Parra <[email protected]> * Rework SetStatus * Inline TryEmitStatus and use a proper context * Fix copyright notice * Fix bug in statusFromStatusCode * Test statusFromResponse * Add link to Slack API schema * Refactor statusFromStatusCode * Expand comment for Ping() * Add basic check for status in slack test * Address nits --------- Co-authored-by: Alan Parra <[email protected]>
- Loading branch information
1 parent
548a4ea
commit aec3669
Showing
11 changed files
with
286 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 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,28 @@ | ||
/* | ||
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 common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
// StatusSink defines a destination for PluginStatus | ||
type StatusSink interface { | ||
Emit(ctx context.Context, s types.PluginStatus) error | ||
} |
This file contains 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 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 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 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 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,55 @@ | ||
/* | ||
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 slack | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
func statusFromStatusCode(httpCode int) types.PluginStatus { | ||
var code types.PluginStatusCode | ||
switch { | ||
case httpCode == http.StatusUnauthorized: | ||
code = types.PluginStatusCode_UNAUTHORIZED | ||
case httpCode >= 200 && httpCode < 400: | ||
code = types.PluginStatusCode_RUNNING | ||
default: | ||
code = types.PluginStatusCode_OTHER_ERROR | ||
} | ||
return &types.PluginStatusV1{Code: code} | ||
} | ||
|
||
// statusFromResponse tries to map a Slack API error string | ||
// to a PluginStatus. | ||
// | ||
// Ref: https://github.com/slackapi/slack-api-specs/blob/bc08db49625630e3585bf2f1322128ea04f2a7f3/web-api/slack_web_openapi_v2.json | ||
func statusFromResponse(resp *APIResponse) types.PluginStatus { | ||
if resp.Ok { | ||
return &types.PluginStatusV1{Code: types.PluginStatusCode_RUNNING} | ||
} | ||
|
||
code := types.PluginStatusCode_OTHER_ERROR | ||
switch resp.Error { | ||
case "channel_not_found", "not_in_channel": | ||
code = types.PluginStatusCode_SLACK_NOT_IN_CHANNEL | ||
case "token_expired", "not_authed", "invalid_auth", "account_inactive", "token_revoked", "no_permission", "org_login_required": | ||
code = types.PluginStatusCode_UNAUTHORIZED | ||
} | ||
return &types.PluginStatusV1{Code: code} | ||
} |
This file contains 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,93 @@ | ||
/* | ||
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 slack | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
func TestStatusFromStatusCode(t *testing.T) { | ||
testCases := []struct { | ||
httpCode int | ||
want types.PluginStatusCode | ||
}{ | ||
{ | ||
httpCode: http.StatusOK, | ||
want: types.PluginStatusCode_RUNNING, | ||
}, | ||
{ | ||
httpCode: http.StatusNoContent, | ||
want: types.PluginStatusCode_RUNNING, | ||
}, | ||
|
||
{ | ||
httpCode: http.StatusUnauthorized, | ||
want: types.PluginStatusCode_UNAUTHORIZED, | ||
}, | ||
{ | ||
httpCode: http.StatusInternalServerError, | ||
want: types.PluginStatusCode_OTHER_ERROR, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%d", tc.httpCode), func(t *testing.T) { | ||
require.Equal(t, tc.want, statusFromStatusCode(tc.httpCode).GetCode()) | ||
}) | ||
} | ||
} | ||
|
||
func TestStatusFromResponse(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
response *APIResponse | ||
want types.PluginStatusCode | ||
}{ | ||
{ | ||
name: "ok", | ||
response: &APIResponse{Ok: true}, | ||
want: types.PluginStatusCode_RUNNING, | ||
}, | ||
{ | ||
name: "not_in_channel", | ||
response: &APIResponse{Error: "not_in_channel"}, | ||
want: types.PluginStatusCode_SLACK_NOT_IN_CHANNEL, | ||
}, | ||
{ | ||
name: "unauthorized", | ||
response: &APIResponse{Error: "token_revoked"}, | ||
want: types.PluginStatusCode_UNAUTHORIZED, | ||
}, | ||
{ | ||
name: "other", | ||
response: &APIResponse{Error: "some_error"}, | ||
want: types.PluginStatusCode_OTHER_ERROR, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
require.Equal(t, tc.want, statusFromResponse(tc.response).GetCode()) | ||
}) | ||
} | ||
} |
Oops, something went wrong.