mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-04 02:21:02 +08:00
* Vendor slack plugin and supporting libraries * Fix up plugin integration tests (wip) * Run GCI on vendored code * Use newtype instead of type alias golangci-lint currently panics on this, "skip-files" et al don't help, as it is a linter panic, not an error See https://github.com/dominikh/go-tools/commit/d717045480130461e1e777ead7f2981621f0d9f0 * Remove long-runing plugins tests from difftest * Move access plugin tests to unit-tests-integrations
119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
/*
|
|
Copyright 2021 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 integration
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/api/client"
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/api/types/events"
|
|
)
|
|
|
|
// Client is a wrapper around *client.Client with some additional methods helpful for testing.
|
|
type Client struct {
|
|
*client.Client
|
|
}
|
|
|
|
// SubmitAccessRequestReview is a simpler version of SubmitAccessReview.
|
|
func (api *Client) SubmitAccessRequestReview(ctx context.Context, reqID string, review types.AccessReview) error {
|
|
_, err := api.SubmitAccessReview(ctx, types.AccessReviewSubmission{
|
|
RequestID: reqID,
|
|
Review: review,
|
|
})
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
// ApproveAccessRequest sets an access request state to APPROVED.
|
|
func (api *Client) ApproveAccessRequest(ctx context.Context, reqID, reason string) error {
|
|
update := types.AccessRequestUpdate{
|
|
RequestID: reqID,
|
|
State: types.RequestState_APPROVED,
|
|
Reason: reason,
|
|
}
|
|
return api.SetAccessRequestState(ctx, update)
|
|
}
|
|
|
|
// ApproveAccessRequest sets an access request state to DENIED.
|
|
func (api *Client) DenyAccessRequest(ctx context.Context, reqID, reason string) error {
|
|
update := types.AccessRequestUpdate{
|
|
RequestID: reqID,
|
|
State: types.RequestState_DENIED,
|
|
Reason: reason,
|
|
}
|
|
return api.SetAccessRequestState(ctx, update)
|
|
}
|
|
|
|
// GetAccessRequest loads an access request.
|
|
func (api *Client) GetAccessRequest(ctx context.Context, reqID string) (types.AccessRequest, error) {
|
|
requests, err := api.GetAccessRequests(ctx, types.AccessRequestFilter{ID: reqID})
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
if len(requests) == 0 {
|
|
return nil, trace.NotFound("request %q is not found", reqID)
|
|
}
|
|
return requests[0], nil
|
|
}
|
|
|
|
// PollAccessRequestPluginData waits until plugin data for a give request became available.
|
|
func (api *Client) PollAccessRequestPluginData(ctx context.Context, plugin, reqID string) (map[string]string, error) {
|
|
filter := types.PluginDataFilter{
|
|
Kind: types.KindAccessRequest,
|
|
Resource: reqID,
|
|
Plugin: plugin,
|
|
}
|
|
for {
|
|
pluginDatas, err := api.GetPluginData(ctx, filter)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
if len(pluginDatas) > 0 {
|
|
pluginData := pluginDatas[0]
|
|
entry := pluginData.Entries()[plugin]
|
|
if entry != nil {
|
|
return entry.Data, nil
|
|
}
|
|
}
|
|
time.Sleep(25 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// SearchAccessRequestEvents searches for recent access request events in audit log.
|
|
func (api *Client) SearchAccessRequestEvents(ctx context.Context, reqID string) ([]*events.AccessRequestCreate, error) {
|
|
auditEvents, _, err := api.SearchEvents(
|
|
ctx,
|
|
time.Now().UTC().AddDate(0, -1, 0),
|
|
time.Now().UTC(),
|
|
"default",
|
|
[]string{"access_request.update"},
|
|
100,
|
|
types.EventOrderAscending,
|
|
"",
|
|
)
|
|
result := make([]*events.AccessRequestCreate, 0, len(auditEvents))
|
|
for _, event := range auditEvents {
|
|
if event, ok := event.(*events.AccessRequestCreate); ok && event.RequestID == reqID {
|
|
result = append(result, event)
|
|
}
|
|
}
|
|
return result, trace.Wrap(err)
|
|
}
|