-
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.
Add Federations, Federation Certificates (#8)
- Loading branch information
Showing
17 changed files
with
1,568 additions
and
45 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,24 @@ | ||
# 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. | ||
|
||
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... | ||
``` |
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,3 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIICmzCCAYMCBgGI6ANFczANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZtYXN0ZXIwHhcNMjMwNjIzMTEyNjQ4WhcNMzMwNjIzMTEyODI4WjARMQ8wDQYDVQQDDAZtYXN0ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC04rOaDpre/MucE3HXVCnAnpqIqQOeMn696AW2FATnI26x1BsxVAGjcrheAOIu+CxC28m48Ah4+SiTEk/u2X/WbGTd/1GZooz37cge0AWMQGyh8ysZRd6q06kg4QGD1iUtdQyHioMbSr9pPne2QQgSX5/gM9XDuA6dpG9Yv0PIPLFlk3BIUL1qEfUiYbDlrunkN/y4XromJaJPpgXKWraH194bqcgXGQLrCqicKwsRBoQJHg3ODWHjHFOwYODJ1XBsRcAue4J88PKiPV1tZNPVczMptrkqGBYTgOYGjKXGe5EH50RJE4/3Ynurz2s34DSDVJhJOYtGwpfeSuU3i3mVAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAGAweCuWJmJXMUdRtgoFIiu6BGotDX5sA/VOm4CRsEXV7/qnBagrAPkRz86KGm4lOPL0X+I13JQh4/OB1gxnPN+BXhNtCWCoj1wA3/BWjs1ow/gaVXzwdy+1mbc/sUBudsLq2Yqs54GgeYsTBKMVpSLKiRg1NebEFlqFmG2hjPzYg1QHL4VBusMQgqt7TTnOfGtdT3Ss9TKGRQ+iwfNL0BtSAKaTRdhNVU4lDYUs788Kw5od/uJj0wTICKO5/PrkX7Uy42+fyU+4SvJynPOy+M+z+s08JC9+eYXixfeeFG1nNWR+DIKXcXaSwNQW+8RweGbOJxQ2BoUKtl0NCHrvxJw= | ||
-----END CERTIFICATE----- |
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,117 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/selectel/iam-go" | ||
"github.com/selectel/iam-go/service/federations/saml" | ||
"github.com/selectel/iam-go/service/federations/saml/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" | ||
certificateFileName = "cert.crt" | ||
|
||
userEmail = "testmail@example.com" | ||
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.SAMLFederations | ||
federationsCertificatesAPI := federationsAPI.Certificates | ||
usersAPI := iamClient.Users | ||
|
||
ctx := context.Background() | ||
|
||
federation, err := federationsAPI.Create(ctx, saml.CreateRequest{ | ||
Name: federationName, | ||
Description: federationDescription, | ||
Issuer: "http://localhost:8080/realms/master", | ||
SSOUrl: "http://localhost:8080/realms/master/protocol/saml", | ||
SessionMaxAgeHours: 24, | ||
SignAuthnRequests: true, | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Printf("Step 1: Created Federation Name: %s ID: %s\n", federation.Name, federation.ID) | ||
|
||
cert, err := os.ReadFile(certificateFileName) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
certificate, err := federationsCertificatesAPI.Create(ctx, federation.ID, certificates.CreateRequest{ | ||
Name: certificateName, | ||
Description: certificateDescription, | ||
Data: string(cert), | ||
}) | ||
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, saml.UpdateRequest{ | ||
Name: updatedFederationName, | ||
Description: &updatedFederationDescription, | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println("Step 4: Updated Federation Name and Description") | ||
|
||
if deleteAfterRun { | ||
// Removing User and Federation Certificate is unnecessary because removal of Federation | ||
// also deletes its Certificate and all attached Users | ||
fmt.Printf("Step 5: Deleting Federation with ID: %s\n", federation.ID) | ||
if err = federationsAPI.Delete(ctx, federation.ID); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
} |
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,2 @@ | ||
// Package certificates provides a set of functions for interacting with the Selectel Federations Certificates API. | ||
package certificates |
Oops, something went wrong.