Ready to level-up your engineering skills? Join a Pulumi Workshop. Register Now

Role

Allows for creating and managing roles within Keycloak.

Roles allow you define privileges within Keycloak and map them to users and groups.

Example Usage

Realm Role)

using Pulumi;
using Keycloak = Pulumi.Keycloak;

class MyStack : Stack
{
    public MyStack()
    {
        var realm = new Keycloak.Realm("realm", new Keycloak.RealmArgs
        {
            Realm = "my-realm",
            Enabled = true,
        });
        var realmRole = new Keycloak.Role("realmRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
            Description = "My Realm Role",
        });
    }

}
package main

import (
    "github.com/pulumi/pulumi-keycloak/sdk/v3/go/keycloak"
    "github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        realm, err := keycloak.NewRealm(ctx, "realm", &keycloak.RealmArgs{
            Realm:   pulumi.String("my-realm"),
            Enabled: pulumi.Bool(true),
        })
        if err != nil {
            return err
        }
        _, err = keycloak.NewRole(ctx, "realmRole", &keycloak.RoleArgs{
            RealmId:     realm.ID(),
            Description: pulumi.String("My Realm Role"),
        })
        if err != nil {
            return err
        }
        return nil
    })
}
import pulumi
import pulumi_keycloak as keycloak

realm = keycloak.Realm("realm",
    realm="my-realm",
    enabled=True)
realm_role = keycloak.Role("realmRole",
    realm_id=realm.id,
    description="My Realm Role")
import * as pulumi from "@pulumi/pulumi";
import * as keycloak from "@pulumi/keycloak";

const realm = new keycloak.Realm("realm", {
    realm: "my-realm",
    enabled: true,
});
const realmRole = new keycloak.Role("realmRole", {
    realmId: realm.id,
    description: "My Realm Role",
});

Client Role)

using Pulumi;
using Keycloak = Pulumi.Keycloak;

class MyStack : Stack
{
    public MyStack()
    {
        var realm = new Keycloak.Realm("realm", new Keycloak.RealmArgs
        {
            Realm = "my-realm",
            Enabled = true,
        });
        var openidClient = new Keycloak.OpenId.Client("openidClient", new Keycloak.OpenId.ClientArgs
        {
            RealmId = realm.Id,
            ClientId = "client",
            Enabled = true,
            AccessType = "CONFIDENTIAL",
            ValidRedirectUris = 
            {
                "http://localhost:8080/openid-callback",
            },
        });
        var clientRole = new Keycloak.Role("clientRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
            ClientId = keycloak_client.Openid_client.Id,
            Description = "My Client Role",
        });
    }

}
package main

import (
    "github.com/pulumi/pulumi-keycloak/sdk/v3/go/keycloak"
    "github.com/pulumi/pulumi-keycloak/sdk/v3/go/keycloak/openid"
    "github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        realm, err := keycloak.NewRealm(ctx, "realm", &keycloak.RealmArgs{
            Realm:   pulumi.String("my-realm"),
            Enabled: pulumi.Bool(true),
        })
        if err != nil {
            return err
        }
        _, err = openid.NewClient(ctx, "openidClient", &openid.ClientArgs{
            RealmId:    realm.ID(),
            ClientId:   pulumi.String("client"),
            Enabled:    pulumi.Bool(true),
            AccessType: pulumi.String("CONFIDENTIAL"),
            ValidRedirectUris: pulumi.StringArray{
                pulumi.String("http://localhost:8080/openid-callback"),
            },
        })
        if err != nil {
            return err
        }
        _, err = keycloak.NewRole(ctx, "clientRole", &keycloak.RoleArgs{
            RealmId:     realm.ID(),
            ClientId:    pulumi.Any(keycloak_client.Openid_client.Id),
            Description: pulumi.String("My Client Role"),
        })
        if err != nil {
            return err
        }
        return nil
    })
}
import pulumi
import pulumi_keycloak as keycloak

realm = keycloak.Realm("realm",
    realm="my-realm",
    enabled=True)
openid_client = keycloak.openid.Client("openidClient",
    realm_id=realm.id,
    client_id="client",
    enabled=True,
    access_type="CONFIDENTIAL",
    valid_redirect_uris=["http://localhost:8080/openid-callback"])
client_role = keycloak.Role("clientRole",
    realm_id=realm.id,
    client_id=keycloak_client["openid_client"]["id"],
    description="My Client Role")
import * as pulumi from "@pulumi/pulumi";
import * as keycloak from "@pulumi/keycloak";

const realm = new keycloak.Realm("realm", {
    realm: "my-realm",
    enabled: true,
});
const openidClient = new keycloak.openid.Client("openidClient", {
    realmId: realm.id,
    clientId: "client",
    enabled: true,
    accessType: "CONFIDENTIAL",
    validRedirectUris: ["http://localhost:8080/openid-callback"],
});
const clientRole = new keycloak.Role("clientRole", {
    realmId: realm.id,
    clientId: keycloak_client.openid_client.id,
    description: "My Client Role",
});

Composite Role)

using Pulumi;
using Keycloak = Pulumi.Keycloak;

class MyStack : Stack
{
    public MyStack()
    {
        var realm = new Keycloak.Realm("realm", new Keycloak.RealmArgs
        {
            Realm = "my-realm",
            Enabled = true,
        });
        // realm roles
        var createRole = new Keycloak.Role("createRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
        });
        var readRole = new Keycloak.Role("readRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
        });
        var updateRole = new Keycloak.Role("updateRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
        });
        var deleteRole = new Keycloak.Role("deleteRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
        });
        // client role
        var openidClient = new Keycloak.OpenId.Client("openidClient", new Keycloak.OpenId.ClientArgs
        {
            RealmId = realm.Id,
            ClientId = "client",
            Enabled = true,
            AccessType = "CONFIDENTIAL",
            ValidRedirectUris = 
            {
                "http://localhost:8080/openid-callback",
            },
        });
        var clientRole = new Keycloak.Role("clientRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
            ClientId = keycloak_client.Openid_client.Id,
            Description = "My Client Role",
        });
        var adminRole = new Keycloak.Role("adminRole", new Keycloak.RoleArgs
        {
            RealmId = realm.Id,
            CompositeRoles = 
            {
                createRole.Id,
                readRole.Id,
                updateRole.Id,
                deleteRole.Id,
                clientRole.Id,
            },
        });
    }

}
package main

import (
    "github.com/pulumi/pulumi-keycloak/sdk/v3/go/keycloak"
    "github.com/pulumi/pulumi-keycloak/sdk/v3/go/keycloak/openid"
    "github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        realm, err := keycloak.NewRealm(ctx, "realm", &keycloak.RealmArgs{
            Realm:   pulumi.String("my-realm"),
            Enabled: pulumi.Bool(true),
        })
        if err != nil {
            return err
        }
        createRole, err := keycloak.NewRole(ctx, "createRole", &keycloak.RoleArgs{
            RealmId: realm.ID(),
        })
        if err != nil {
            return err
        }
        readRole, err := keycloak.NewRole(ctx, "readRole", &keycloak.RoleArgs{
            RealmId: realm.ID(),
        })
        if err != nil {
            return err
        }
        updateRole, err := keycloak.NewRole(ctx, "updateRole", &keycloak.RoleArgs{
            RealmId: realm.ID(),
        })
        if err != nil {
            return err
        }
        deleteRole, err := keycloak.NewRole(ctx, "deleteRole", &keycloak.RoleArgs{
            RealmId: realm.ID(),
        })
        if err != nil {
            return err
        }
        _, err = openid.NewClient(ctx, "openidClient", &openid.ClientArgs{
            RealmId:    realm.ID(),
            ClientId:   pulumi.String("client"),
            Enabled:    pulumi.Bool(true),
            AccessType: pulumi.String("CONFIDENTIAL"),
            ValidRedirectUris: pulumi.StringArray{
                pulumi.String("http://localhost:8080/openid-callback"),
            },
        })
        if err != nil {
            return err
        }
        clientRole, err := keycloak.NewRole(ctx, "clientRole", &keycloak.RoleArgs{
            RealmId:     realm.ID(),
            ClientId:    pulumi.Any(keycloak_client.Openid_client.Id),
            Description: pulumi.String("My Client Role"),
        })
        if err != nil {
            return err
        }
        _, err = keycloak.NewRole(ctx, "adminRole", &keycloak.RoleArgs{
            RealmId: realm.ID(),
            CompositeRoles: pulumi.StringArray{
                createRole.ID(),
                readRole.ID(),
                updateRole.ID(),
                deleteRole.ID(),
                clientRole.ID(),
            },
        })
        if err != nil {
            return err
        }
        return nil
    })
}
import pulumi
import pulumi_keycloak as keycloak

realm = keycloak.Realm("realm",
    realm="my-realm",
    enabled=True)
# realm roles
create_role = keycloak.Role("createRole", realm_id=realm.id)
read_role = keycloak.Role("readRole", realm_id=realm.id)
update_role = keycloak.Role("updateRole", realm_id=realm.id)
delete_role = keycloak.Role("deleteRole", realm_id=realm.id)
# client role
openid_client = keycloak.openid.Client("openidClient",
    realm_id=realm.id,
    client_id="client",
    enabled=True,
    access_type="CONFIDENTIAL",
    valid_redirect_uris=["http://localhost:8080/openid-callback"])
client_role = keycloak.Role("clientRole",
    realm_id=realm.id,
    client_id=keycloak_client["openid_client"]["id"],
    description="My Client Role")
admin_role = keycloak.Role("adminRole",
    realm_id=realm.id,
    composite_roles=[
        create_role.id,
        read_role.id,
        update_role.id,
        delete_role.id,
        client_role.id,
    ])
import * as pulumi from "@pulumi/pulumi";
import * as keycloak from "@pulumi/keycloak";

const realm = new keycloak.Realm("realm", {
    realm: "my-realm",
    enabled: true,
});
// realm roles
const createRole = new keycloak.Role("createRole", {realmId: realm.id});
const readRole = new keycloak.Role("readRole", {realmId: realm.id});
const updateRole = new keycloak.Role("updateRole", {realmId: realm.id});
const deleteRole = new keycloak.Role("deleteRole", {realmId: realm.id});
// client role
const openidClient = new keycloak.openid.Client("openidClient", {
    realmId: realm.id,
    clientId: "client",
    enabled: true,
    accessType: "CONFIDENTIAL",
    validRedirectUris: ["http://localhost:8080/openid-callback"],
});
const clientRole = new keycloak.Role("clientRole", {
    realmId: realm.id,
    clientId: keycloak_client.openid_client.id,
    description: "My Client Role",
});
const adminRole = new keycloak.Role("adminRole", {
    realmId: realm.id,
    compositeRoles: [
        createRole.id,
        readRole.id,
        updateRole.id,
        deleteRole.id,
        clientRole.id,
    ],
});

Create a Role Resource

new Role(name: string, args: RoleArgs, opts?: CustomResourceOptions);
def Role(resource_name: str, opts: Optional[ResourceOptions] = None, client_id: Optional[str] = None, composite_roles: Optional[Sequence[str]] = None, description: Optional[str] = None, name: Optional[str] = None, realm_id: Optional[str] = None)
func NewRole(ctx *Context, name string, args RoleArgs, opts ...ResourceOption) (*Role, error)
public Role(string name, RoleArgs args, CustomResourceOptions? opts = null)
name string
The unique name of the resource.
args RoleArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
opts ResourceOptions
A bag of options that control this resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args RoleArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args RoleArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.

Role Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Programming Model docs.

Inputs

The Role resource accepts the following input properties:

RealmId string

The realm this role exists within.

ClientId string

When specified, this role will be created as a client role attached to the client with the provided ID

CompositeRoles List<string>

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

Description string

The description of the role

Name string

The name of the role

RealmId string

The realm this role exists within.

ClientId string

When specified, this role will be created as a client role attached to the client with the provided ID

CompositeRoles []string

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

Description string

The description of the role

Name string

The name of the role

realmId string

The realm this role exists within.

clientId string

When specified, this role will be created as a client role attached to the client with the provided ID

compositeRoles string[]

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

description string

The description of the role

name string

The name of the role

realm_id str

The realm this role exists within.

client_id str

When specified, this role will be created as a client role attached to the client with the provided ID

composite_roles Sequence[str]

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

description str

The description of the role

name str

The name of the role

Outputs

All input properties are implicitly available as output properties. Additionally, the Role resource produces the following output properties:

Id string
The provider-assigned unique ID for this managed resource.
Id string
The provider-assigned unique ID for this managed resource.
id string
The provider-assigned unique ID for this managed resource.
id str
The provider-assigned unique ID for this managed resource.

Look up an Existing Role Resource

Get an existing Role resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: RoleState, opts?: CustomResourceOptions): Role
@staticmethod
def get(resource_name: str, id: str, opts: Optional[ResourceOptions] = None, client_id: Optional[str] = None, composite_roles: Optional[Sequence[str]] = None, description: Optional[str] = None, name: Optional[str] = None, realm_id: Optional[str] = None) -> Role
func GetRole(ctx *Context, name string, id IDInput, state *RoleState, opts ...ResourceOption) (*Role, error)
public static Role Get(string name, Input<string> id, RoleState? state, CustomResourceOptions? opts = null)
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.

The following state arguments are supported:

ClientId string

When specified, this role will be created as a client role attached to the client with the provided ID

CompositeRoles List<string>

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

Description string

The description of the role

Name string

The name of the role

RealmId string

The realm this role exists within.

ClientId string

When specified, this role will be created as a client role attached to the client with the provided ID

CompositeRoles []string

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

Description string

The description of the role

Name string

The name of the role

RealmId string

The realm this role exists within.

clientId string

When specified, this role will be created as a client role attached to the client with the provided ID

compositeRoles string[]

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

description string

The description of the role

name string

The name of the role

realmId string

The realm this role exists within.

client_id str

When specified, this role will be created as a client role attached to the client with the provided ID

composite_roles Sequence[str]

When specified, this role will be a composite role, composed of all roles that have an ID present within this list.

description str

The description of the role

name str

The name of the role

realm_id str

The realm this role exists within.

Import

Roles can be imported using the format {{realm_id}}/{{role_id}}, where role_id is the unique ID that Keycloak assigns to the role. The ID is not easy to find in the GUI, but it appears in the URL when editing the role. Examplebash

 $ pulumi import keycloak:index/role:Role role my-realm/7e8cf32a-8acb-4d34-89c4-04fb1d10ccad

Package Details

Repository
https://github.com/pulumi/pulumi-keycloak
License
Apache-2.0
Notes
This Pulumi package is based on the keycloak Terraform Provider.