Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Kuznetsov committed Jul 24, 2024
1 parent cd9d6a0 commit 9ee8a1f
Show file tree
Hide file tree
Showing 13 changed files with 1,623 additions and 30 deletions.
28 changes: 28 additions & 0 deletions examples/federation-with-user/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Create Federation with Certificate & add User

This example program demonstrates how to manage creating and deleting Federation with Certificate and assigning Users.

The part of deleting is disabled by `deleteAfterRun` variable.

## Running this example

Running this file will execute the following operations:

1. **Create Federation:** Create is used to create a new Federation.
2. **Create Certificate for Federation:** Create is used to create a new Certificate for Federation.
3. **Create federated User:** Create is used to create a new federated User.
4. **Update Federation:** Updates the Federation Name and Description.
5. **(Delete Federation):** _(disabled by default)_ Delete a just-created Federation on a previous step.
6. **(Delete Federation Certificate):** _(disabled by default)_ Delete a just-created Federation Certificate on a previous step.
7. **(Delete User):** _(disabled by default)_ Delete a just-created federated User on a previous step.

You should see an output like the following:
```
Step 1: Created Federation Name: federation_name ID: 1a2b3c...
Step 2: Created Certificate for Federation ID: 12345_3... Federation ID: 1a2b3c...
Step 3: Created federated User ID: 54321_2... Keystone ID: 1c2b3a...
Step 4: Updated Federation Name and Description
Step 5: Deleting Federation with ID: 1a2b3c...
Step 6: Deleting Federation Certificate with ID: 12345_3...
Step 6: Deleting User with ID: 54321_2...
```
123 changes: 123 additions & 0 deletions examples/federation-with-user/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"context"
"fmt"

"github.com/selectel/iam-go"
"github.com/selectel/iam-go/service/federations"
"github.com/selectel/iam-go/service/federations/certificates"
"github.com/selectel/iam-go/service/roles"
"github.com/selectel/iam-go/service/users"
)

var (
// KeystoneToken
token = "gAAAAA..."
deleteAfterRun = false

// Prefix to be added to User-Agent.
prefix = "iam-go"

federationName = "federation_name"
federationDescription = "federation_description"
updatedFederationName = "new_federation_name"
updatedFederationDescription = "new_federation_description"

certificateName = "certificate name"
certificateDescription = "certificate description"
certificateData = "cert"

userEmail = "[email protected]"
userExternalID = "some_id"
)

func main() {
// Create a new IAM client.
iamClient, err := iam.New(
iam.WithAuthOpts(&iam.AuthOpts{KeystoneToken: token}),
iam.WithUserAgentPrefix(prefix),
)
if err != nil {
fmt.Println(err)
return
}

federationsAPI := iamClient.Federations
federationsCertificatesAPI := iamClient.FederationsCertificates
usersAPI := iamClient.Users

ctx := context.Background()

federation, err := federationsAPI.Create(ctx, federations.CreateRequest{
Federation: federations.Federation{
Name: federationName,
Description: federationDescription,
Issuer: "http://localhost:8080/realms/master",
SSOUrl: "http://localhost:8080/realms/master/protocol/saml",
SignAuthnRequests: true,
ForceAuthn: true,
SessionMaxAgeHours: 100,
},
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Step 1: Created Federation Name: %s ID: %s\n", federation.Name, federation.ID)

certificate, err := federationsCertificatesAPI.Create(ctx, federation.ID, certificates.CreateRequest{
Name: certificateName,
Description: certificateDescription,
Data: certificateData,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Step 2: Created Certificate for Federation ID: %s Federation ID: %s\n", certificate.ID, federation.ID)

user, err := usersAPI.Create(ctx, users.CreateRequest{
AuthType: users.Federated,
Email: userEmail,
Federation: &users.Federation{
ExternalID: userExternalID,
ID: federation.ID,
},
Roles: []roles.Role{{Scope: roles.Account, RoleName: roles.Reader}},
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Step 3: Created federated User ID: %s Keystone ID: %s\n", user.ID, user.KeystoneID)

err = federationsAPI.Update(ctx, federation.ID, federations.UpdateRequest{
Federation: federations.Federation{
Name: updatedFederationName,
Description: updatedFederationDescription,
},
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Step 4: Updated Federation Name and Description")

if deleteAfterRun {
fmt.Printf("Step 5: Deleting Federation with ID: %s\n", federation.ID)
if err = federationsAPI.Delete(ctx, federation.ID); err != nil {
fmt.Println(err)
}

fmt.Printf("Step 6: Deleting Federation Certificate with ID: %s\n", certificate.ID)
if err = federationsAPI.Delete(ctx, federation.ID); err != nil {
fmt.Println(err)
}

fmt.Printf("Step 7: Deleting User with ID: %s\n", user.ID)
if err = usersAPI.Delete(ctx, user.ID); err != nil {
fmt.Println(err)
}
}
}
11 changes: 11 additions & 0 deletions iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/selectel/iam-go/iamerrors"
baseclient "github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/federations"
"github.com/selectel/iam-go/service/federations/certificates"
"github.com/selectel/iam-go/service/groups"
"github.com/selectel/iam-go/service/s3credentials"
"github.com/selectel/iam-go/service/serviceusers"
Expand Down Expand Up @@ -57,6 +59,13 @@ type Client struct {

// S3Credentials instance is used to make requests against Selectel IAM API and manage S3 Credentials.
S3Credentials *s3credentials.Service

// Federations instance is used to make requests against Selectel IAM API and manage Federations.
Federations *federations.Service

// FederationsCertificates instance is used to make requests against Selectel IAM API
// and manage Federations Certificates.
FederationsCertificates *certificates.Service
}

type AuthOpts struct {
Expand Down Expand Up @@ -128,6 +137,8 @@ func New(opts ...Option) (*Client, error) {
c.ServiceUsers = serviceusers.New(c.baseClient)
c.Groups = groups.New(c.baseClient)
c.S3Credentials = s3credentials.New(c.baseClient)
c.Federations = federations.New(c.baseClient)
c.FederationsCertificates = certificates.New(c.baseClient)

return c, nil
}
Expand Down
73 changes: 43 additions & 30 deletions iamerrors/iamerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ var (
ErrGroupNotFound = errors.New("GROUP_NOT_FOUND")
ErrUserOrGroupNotFound = errors.New("USER_OR_GROUP_NOT_FOUND")

ErrFederationNameRequired = errors.New("FEDERATION_NAME_REQUIRED")
ErrFederationIDRequired = errors.New("FEDERATION_ID_REQUIRED")
ErrFederationIssuerRequired = errors.New("FEDERATION_ISSUER_REQUIRED")
ErrFederationSSOURLRequired = errors.New("FEDERATION_SSO_URL_REQUIRED")
ErrFederationCertificateIDRequired = errors.New("FEDERATION_CERTIFICATE_ID_REQUIRED")
ErrFederationNotFound = errors.New("FEDERATION_NOT_FOUND")

ErrCredentialNameRequired = errors.New("CREDENTIAL_NAME_REQUIRED")
ErrCredentialAccessKeyRequired = errors.New("CREDENTIAL_ACCESS_KEY_REQUIRED")

Expand All @@ -48,36 +55,42 @@ var (

//nolint:gochecknoglobals // stringToError is not global.
stringToError = map[string]error{
ErrUserNotFound.Error(): ErrUserNotFound,
ErrClientNoAuthOpts.Error(): ErrClientNoAuthOpts,
ErrAuthTokenUnathorized.Error(): ErrAuthTokenUnathorized,
ErrDomainNotFound.Error(): ErrDomainNotFound,
ErrCredentialNotFound.Error(): ErrCredentialNotFound,
ErrProjectNotFound.Error(): ErrProjectNotFound,
ErrUserAlreadyExists.Error(): ErrUserAlreadyExists,
ErrRequestValidationError.Error(): ErrRequestValidationError,
ErrForbidden.Error(): ErrForbidden,
ErrUnauthorized.Error(): ErrUnauthorized,
ErrInternalServerError.Error(): ErrInternalServerError,
ErrCredentialNameRequired.Error(): ErrCredentialNameRequired,
ErrCredentialAccessKeyRequired.Error(): ErrCredentialAccessKeyRequired,
ErrUserIDRequired.Error(): ErrUserIDRequired,
ErrProjectIDRequired.Error(): ErrProjectIDRequired,
ErrGroupIDRequired.Error(): ErrGroupIDRequired,
ErrGroupUserIDsRequired.Error(): ErrGroupUserIDsRequired,
ErrGroupNameRequired.Error(): ErrGroupNameRequired,
ErrGroupRolesRequired.Error(): ErrGroupRolesRequired,
ErrGroupAlreadyExists.Error(): ErrGroupAlreadyExists,
ErrGroupNotFound.Error(): ErrGroupNotFound,
ErrUserOrGroupNotFound.Error(): ErrUserOrGroupNotFound,
ErrServiceUserNameRequired.Error(): ErrServiceUserNameRequired,
ErrServiceUserPasswordRequired.Error(): ErrServiceUserPasswordRequired,
ErrServiceUserRolesRequired.Error(): ErrServiceUserRolesRequired,
ErrUserRolesRequired.Error(): ErrUserRolesRequired,
ErrUserEmailRequired.Error(): ErrUserEmailRequired,
ErrInputDataRequired.Error(): ErrInputDataRequired,
ErrInternalAppError.Error(): ErrInternalAppError,
ErrUnknown.Error(): ErrUnknown,
ErrUserNotFound.Error(): ErrUserNotFound,
ErrClientNoAuthOpts.Error(): ErrClientNoAuthOpts,
ErrAuthTokenUnathorized.Error(): ErrAuthTokenUnathorized,
ErrDomainNotFound.Error(): ErrDomainNotFound,
ErrCredentialNotFound.Error(): ErrCredentialNotFound,
ErrProjectNotFound.Error(): ErrProjectNotFound,
ErrUserAlreadyExists.Error(): ErrUserAlreadyExists,
ErrRequestValidationError.Error(): ErrRequestValidationError,
ErrForbidden.Error(): ErrForbidden,
ErrUnauthorized.Error(): ErrUnauthorized,
ErrInternalServerError.Error(): ErrInternalServerError,
ErrCredentialNameRequired.Error(): ErrCredentialNameRequired,
ErrCredentialAccessKeyRequired.Error(): ErrCredentialAccessKeyRequired,
ErrUserIDRequired.Error(): ErrUserIDRequired,
ErrProjectIDRequired.Error(): ErrProjectIDRequired,
ErrGroupIDRequired.Error(): ErrGroupIDRequired,
ErrGroupUserIDsRequired.Error(): ErrGroupUserIDsRequired,
ErrGroupNameRequired.Error(): ErrGroupNameRequired,
ErrGroupRolesRequired.Error(): ErrGroupRolesRequired,
ErrGroupAlreadyExists.Error(): ErrGroupAlreadyExists,
ErrGroupNotFound.Error(): ErrGroupNotFound,
ErrFederationNameRequired.Error(): ErrFederationNameRequired,
ErrFederationIDRequired.Error(): ErrFederationIDRequired,
ErrFederationIssuerRequired.Error(): ErrFederationIssuerRequired,
ErrFederationSSOURLRequired.Error(): ErrFederationSSOURLRequired,
ErrFederationCertificateIDRequired.Error(): ErrFederationCertificateIDRequired,
ErrFederationNotFound.Error(): ErrFederationNotFound,
ErrUserOrGroupNotFound.Error(): ErrUserOrGroupNotFound,
ErrServiceUserNameRequired.Error(): ErrServiceUserNameRequired,
ErrServiceUserPasswordRequired.Error(): ErrServiceUserPasswordRequired,
ErrServiceUserRolesRequired.Error(): ErrServiceUserRolesRequired,
ErrUserRolesRequired.Error(): ErrUserRolesRequired,
ErrUserEmailRequired.Error(): ErrUserEmailRequired,
ErrInputDataRequired.Error(): ErrInputDataRequired,
ErrInternalAppError.Error(): ErrInternalAppError,
ErrUnknown.Error(): ErrUnknown,
}
)

Expand Down
Loading

0 comments on commit 9ee8a1f

Please sign in to comment.