2018-02-14 01:19:17 +00:00
|
|
|
/*
|
2021-04-15 17:10:25 +00:00
|
|
|
Copyright 2018-2021 Gravitational, Inc.
|
2018-02-14 01:19:17 +00:00
|
|
|
|
|
|
|
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 main
|
|
|
|
|
|
|
|
import (
|
2020-04-08 17:30:12 +00:00
|
|
|
"context"
|
2020-11-11 00:36:26 +00:00
|
|
|
"fmt"
|
2018-02-14 01:19:17 +00:00
|
|
|
"log"
|
2021-04-15 17:10:25 +00:00
|
|
|
|
2021-07-30 22:34:19 +00:00
|
|
|
"github.com/gravitational/teleport/api/client"
|
|
|
|
"github.com/gravitational/teleport/api/types"
|
2021-04-15 17:10:25 +00:00
|
|
|
|
2022-01-19 23:44:48 +00:00
|
|
|
"github.com/google/uuid"
|
2018-02-14 01:19:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-04-15 17:10:25 +00:00
|
|
|
ctx := context.Background()
|
2020-11-11 00:36:26 +00:00
|
|
|
log.Printf("Starting Teleport client...")
|
2021-04-15 17:10:25 +00:00
|
|
|
|
|
|
|
cfg := client.Config{
|
|
|
|
Credentials: []client.Credentials{
|
|
|
|
client.LoadProfile("", ""),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clt, err := client.New(ctx, cfg)
|
2018-02-14 01:19:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create client: %v", err)
|
|
|
|
}
|
2021-04-15 17:10:25 +00:00
|
|
|
defer clt.Close()
|
2018-02-14 01:19:17 +00:00
|
|
|
|
2021-04-15 17:10:25 +00:00
|
|
|
if err := demoClient(ctx, clt); err != nil {
|
|
|
|
log.Printf("error in demoClient: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 01:19:17 +00:00
|
|
|
|
2021-04-15 17:10:25 +00:00
|
|
|
func demoClient(ctx context.Context, clt *client.Client) (err error) {
|
|
|
|
// Create a new access request for the `access-admin` user to use the `admin` role.
|
2022-01-19 23:44:48 +00:00
|
|
|
accessReq, err := types.NewAccessRequest(uuid.New().String(), "access-admin", "admin")
|
2021-04-15 17:10:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to make new access request: %w", err)
|
|
|
|
}
|
|
|
|
if err = clt.CreateAccessRequest(ctx, accessReq); err != nil {
|
|
|
|
return fmt.Errorf("failed to create access request: %w", err)
|
|
|
|
}
|
|
|
|
log.Printf("Created access request: %v", accessReq)
|
2018-02-14 01:19:17 +00:00
|
|
|
|
2021-04-15 17:10:25 +00:00
|
|
|
// Approve the access request as if this was another party.
|
|
|
|
if err = clt.SetAccessRequestState(ctx, types.AccessRequestUpdate{
|
|
|
|
RequestID: accessReq.GetName(),
|
|
|
|
State: types.RequestState_APPROVED,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("failed to accept request: %w", err)
|
|
|
|
}
|
|
|
|
log.Printf("Approved access request")
|
|
|
|
|
|
|
|
if err := clt.DeleteAccessRequest(ctx, accessReq.GetName()); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete access request: %w", err)
|
|
|
|
}
|
|
|
|
log.Println("Deleted access request")
|
2018-02-14 01:19:17 +00:00
|
|
|
|
2021-04-15 17:10:25 +00:00
|
|
|
return nil
|
2018-02-14 01:19:17 +00:00
|
|
|
}
|