published on Saturday, Feb 21, 2026 by Pulumi
published on Saturday, Feb 21, 2026 by Pulumi
Allows you to manage JavaScript policies.
JavaScript policies allow you to define conditions using JavaScript code. This provides maximum flexibility for implementing custom authorization logic.
Example Usage
import * as pulumi from "@pulumi/pulumi";
import * as keycloak from "@pulumi/keycloak";
const realm = new keycloak.Realm("realm", {
realm: "my-realm",
enabled: true,
});
const test = new keycloak.openid.Client("test", {
clientId: "client_id",
realmId: realm.id,
accessType: "CONFIDENTIAL",
serviceAccountsEnabled: true,
authorization: {
policyEnforcementMode: "ENFORCING",
},
});
const testClientJsPolicy = new keycloak.openid.ClientJsPolicy("test", {
resourceServerId: test.resourceServerId,
realmId: realm.id,
name: "js_policy",
decisionStrategy: "UNANIMOUS",
logic: "POSITIVE",
code: `var context = evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
evaluation.grant();
}
`,
});
import pulumi
import pulumi_keycloak as keycloak
realm = keycloak.Realm("realm",
realm="my-realm",
enabled=True)
test = keycloak.openid.Client("test",
client_id="client_id",
realm_id=realm.id,
access_type="CONFIDENTIAL",
service_accounts_enabled=True,
authorization={
"policy_enforcement_mode": "ENFORCING",
})
test_client_js_policy = keycloak.openid.ClientJsPolicy("test",
resource_server_id=test.resource_server_id,
realm_id=realm.id,
name="js_policy",
decision_strategy="UNANIMOUS",
logic="POSITIVE",
code="""var context = $evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
$evaluation.grant();
}
""")
package main
import (
"github.com/pulumi/pulumi-keycloak/sdk/v6/go/keycloak"
"github.com/pulumi/pulumi-keycloak/sdk/v6/go/keycloak/openid"
"github.com/pulumi/pulumi/sdk/v3/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
}
test, err := openid.NewClient(ctx, "test", &openid.ClientArgs{
ClientId: pulumi.String("client_id"),
RealmId: realm.ID(),
AccessType: pulumi.String("CONFIDENTIAL"),
ServiceAccountsEnabled: pulumi.Bool(true),
Authorization: &openid.ClientAuthorizationArgs{
PolicyEnforcementMode: pulumi.String("ENFORCING"),
},
})
if err != nil {
return err
}
_, err = openid.NewClientJsPolicy(ctx, "test", &openid.ClientJsPolicyArgs{
ResourceServerId: test.ResourceServerId,
RealmId: realm.ID(),
Name: pulumi.String("js_policy"),
DecisionStrategy: pulumi.String("UNANIMOUS"),
Logic: pulumi.String("POSITIVE"),
Code: pulumi.String(`var context = $evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
$evaluation.grant();
}
`),
})
if err != nil {
return err
}
return nil
})
}
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Keycloak = Pulumi.Keycloak;
return await Deployment.RunAsync(() =>
{
var realm = new Keycloak.Realm("realm", new()
{
RealmName = "my-realm",
Enabled = true,
});
var test = new Keycloak.OpenId.Client("test", new()
{
ClientId = "client_id",
RealmId = realm.Id,
AccessType = "CONFIDENTIAL",
ServiceAccountsEnabled = true,
Authorization = new Keycloak.OpenId.Inputs.ClientAuthorizationArgs
{
PolicyEnforcementMode = "ENFORCING",
},
});
var testClientJsPolicy = new Keycloak.OpenId.ClientJsPolicy("test", new()
{
ResourceServerId = test.ResourceServerId,
RealmId = realm.Id,
Name = "js_policy",
DecisionStrategy = "UNANIMOUS",
Logic = "POSITIVE",
Code = @"var context = $evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
$evaluation.grant();
}
",
});
});
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.keycloak.Realm;
import com.pulumi.keycloak.RealmArgs;
import com.pulumi.keycloak.openid.Client;
import com.pulumi.keycloak.openid.ClientArgs;
import com.pulumi.keycloak.openid.inputs.ClientAuthorizationArgs;
import com.pulumi.keycloak.openid.ClientJsPolicy;
import com.pulumi.keycloak.openid.ClientJsPolicyArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var realm = new Realm("realm", RealmArgs.builder()
.realm("my-realm")
.enabled(true)
.build());
var test = new Client("test", ClientArgs.builder()
.clientId("client_id")
.realmId(realm.id())
.accessType("CONFIDENTIAL")
.serviceAccountsEnabled(true)
.authorization(ClientAuthorizationArgs.builder()
.policyEnforcementMode("ENFORCING")
.build())
.build());
var testClientJsPolicy = new ClientJsPolicy("testClientJsPolicy", ClientJsPolicyArgs.builder()
.resourceServerId(test.resourceServerId())
.realmId(realm.id())
.name("js_policy")
.decisionStrategy("UNANIMOUS")
.logic("POSITIVE")
.code("""
var context = $evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
$evaluation.grant();
}
""")
.build());
}
}
resources:
realm:
type: keycloak:Realm
properties:
realm: my-realm
enabled: true
test:
type: keycloak:openid:Client
properties:
clientId: client_id
realmId: ${realm.id}
accessType: CONFIDENTIAL
serviceAccountsEnabled: true
authorization:
policyEnforcementMode: ENFORCING
testClientJsPolicy:
type: keycloak:openid:ClientJsPolicy
name: test
properties:
resourceServerId: ${test.resourceServerId}
realmId: ${realm.id}
name: js_policy
decisionStrategy: UNANIMOUS
logic: POSITIVE
code: |
var context = $evaluation.getContext();
var identity = context.getIdentity();
var attributes = identity.getAttributes();
var email = attributes.getValue('email').asString(0);
if (email.endsWith('@example.com')) {
$evaluation.grant();
}
Argument Reference
The following arguments are supported:
realm_id- (Required) The realm this policy exists in.resource_server_id- (Required) The ID of the resource server.name- (Required) The name of the policy.decision_strategy- (Required) The decision strategy, can be one ofUNANIMOUS,AFFIRMATIVE, orCONSENSUS.code- (Required) The JavaScript code to execute for this policy.logic- (Optional) The logic, can be one ofPOSITIVEorNEGATIVE. Defaults toPOSITIVE.type- (Optional) The type of the policy. Defaults tojs.description- (Optional) A description for the authorization policy.
Attributes Reference
In addition to the arguments listed above, the following computed attributes are exported:
id- Policy ID representing the JavaScript policy.
Create ClientJsPolicy Resource
Resources are created with functions called constructors. To learn more about declaring and configuring resources, see Resources.
Constructor syntax
new ClientJsPolicy(name: string, args: ClientJsPolicyArgs, opts?: CustomResourceOptions);@overload
def ClientJsPolicy(resource_name: str,
args: ClientJsPolicyArgs,
opts: Optional[ResourceOptions] = None)
@overload
def ClientJsPolicy(resource_name: str,
opts: Optional[ResourceOptions] = None,
code: Optional[str] = None,
decision_strategy: Optional[str] = None,
realm_id: Optional[str] = None,
resource_server_id: Optional[str] = None,
description: Optional[str] = None,
logic: Optional[str] = None,
name: Optional[str] = None,
type: Optional[str] = None)func NewClientJsPolicy(ctx *Context, name string, args ClientJsPolicyArgs, opts ...ResourceOption) (*ClientJsPolicy, error)public ClientJsPolicy(string name, ClientJsPolicyArgs args, CustomResourceOptions? opts = null)
public ClientJsPolicy(String name, ClientJsPolicyArgs args)
public ClientJsPolicy(String name, ClientJsPolicyArgs args, CustomResourceOptions options)
type: keycloak:openid:ClientJsPolicy
properties: # The arguments to resource properties.
options: # Bag of options to control resource's behavior.
Parameters
- name string
- The unique name of the resource.
- args ClientJsPolicyArgs
- The arguments to resource properties.
- opts CustomResourceOptions
- Bag of options to control resource's behavior.
- resource_name str
- The unique name of the resource.
- args ClientJsPolicyArgs
- The arguments to resource properties.
- opts ResourceOptions
- Bag of options to control resource's behavior.
- ctx Context
- Context object for the current deployment.
- name string
- The unique name of the resource.
- args ClientJsPolicyArgs
- The arguments to resource properties.
- opts ResourceOption
- Bag of options to control resource's behavior.
- name string
- The unique name of the resource.
- args ClientJsPolicyArgs
- The arguments to resource properties.
- opts CustomResourceOptions
- Bag of options to control resource's behavior.
- name String
- The unique name of the resource.
- args ClientJsPolicyArgs
- The arguments to resource properties.
- options CustomResourceOptions
- Bag of options to control resource's behavior.
Constructor example
The following reference example uses placeholder values for all input properties.
var clientJsPolicyResource = new Keycloak.OpenId.ClientJsPolicy("clientJsPolicyResource", new()
{
Code = "string",
DecisionStrategy = "string",
RealmId = "string",
ResourceServerId = "string",
Description = "string",
Logic = "string",
Name = "string",
Type = "string",
});
example, err := openid.NewClientJsPolicy(ctx, "clientJsPolicyResource", &openid.ClientJsPolicyArgs{
Code: pulumi.String("string"),
DecisionStrategy: pulumi.String("string"),
RealmId: pulumi.String("string"),
ResourceServerId: pulumi.String("string"),
Description: pulumi.String("string"),
Logic: pulumi.String("string"),
Name: pulumi.String("string"),
Type: pulumi.String("string"),
})
var clientJsPolicyResource = new ClientJsPolicy("clientJsPolicyResource", ClientJsPolicyArgs.builder()
.code("string")
.decisionStrategy("string")
.realmId("string")
.resourceServerId("string")
.description("string")
.logic("string")
.name("string")
.type("string")
.build());
client_js_policy_resource = keycloak.openid.ClientJsPolicy("clientJsPolicyResource",
code="string",
decision_strategy="string",
realm_id="string",
resource_server_id="string",
description="string",
logic="string",
name="string",
type="string")
const clientJsPolicyResource = new keycloak.openid.ClientJsPolicy("clientJsPolicyResource", {
code: "string",
decisionStrategy: "string",
realmId: "string",
resourceServerId: "string",
description: "string",
logic: "string",
name: "string",
type: "string",
});
type: keycloak:openid:ClientJsPolicy
properties:
code: string
decisionStrategy: string
description: string
logic: string
name: string
realmId: string
resourceServerId: string
type: string
ClientJsPolicy Resource Properties
To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.
Inputs
In Python, inputs that are objects can be passed either as argument classes or as dictionary literals.
The ClientJsPolicy resource accepts the following input properties:
- Code string
- Decision
Strategy string - Realm
Id string - Resource
Server stringId - Description string
- Logic string
- Name string
- Type string
- Code string
- Decision
Strategy string - Realm
Id string - Resource
Server stringId - Description string
- Logic string
- Name string
- Type string
- code String
- decision
Strategy String - realm
Id String - resource
Server StringId - description String
- logic String
- name String
- type String
- code string
- decision
Strategy string - realm
Id string - resource
Server stringId - description string
- logic string
- name string
- type string
- code str
- decision_
strategy str - realm_
id str - resource_
server_ strid - description str
- logic str
- name str
- type str
- code String
- decision
Strategy String - realm
Id String - resource
Server StringId - description String
- logic String
- name String
- type String
Outputs
All input properties are implicitly available as output properties. Additionally, the ClientJsPolicy 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 string
- The provider-assigned unique ID for this managed resource.
- id str
- The provider-assigned unique ID for this managed resource.
- id String
- The provider-assigned unique ID for this managed resource.
Look up Existing ClientJsPolicy Resource
Get an existing ClientJsPolicy 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?: ClientJsPolicyState, opts?: CustomResourceOptions): ClientJsPolicy@staticmethod
def get(resource_name: str,
id: str,
opts: Optional[ResourceOptions] = None,
code: Optional[str] = None,
decision_strategy: Optional[str] = None,
description: Optional[str] = None,
logic: Optional[str] = None,
name: Optional[str] = None,
realm_id: Optional[str] = None,
resource_server_id: Optional[str] = None,
type: Optional[str] = None) -> ClientJsPolicyfunc GetClientJsPolicy(ctx *Context, name string, id IDInput, state *ClientJsPolicyState, opts ...ResourceOption) (*ClientJsPolicy, error)public static ClientJsPolicy Get(string name, Input<string> id, ClientJsPolicyState? state, CustomResourceOptions? opts = null)public static ClientJsPolicy get(String name, Output<String> id, ClientJsPolicyState state, CustomResourceOptions options)resources: _: type: keycloak:openid:ClientJsPolicy get: id: ${id}- 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.
- 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.
- Code string
- Decision
Strategy string - Description string
- Logic string
- Name string
- Realm
Id string - Resource
Server stringId - Type string
- Code string
- Decision
Strategy string - Description string
- Logic string
- Name string
- Realm
Id string - Resource
Server stringId - Type string
- code String
- decision
Strategy String - description String
- logic String
- name String
- realm
Id String - resource
Server StringId - type String
- code string
- decision
Strategy string - description string
- logic string
- name string
- realm
Id string - resource
Server stringId - type string
- code str
- decision_
strategy str - description str
- logic str
- name str
- realm_
id str - resource_
server_ strid - type str
- code String
- decision
Strategy String - description String
- logic String
- name String
- realm
Id String - resource
Server StringId - type String
Import
JavaScript policies can be imported using the format: {{realmId}}/{{resourceServerId}}/{{policyId}}.
Example:
$ terraform import keycloak_openid_client_js_policy.test my-realm/3bd4a686-1062-4b59-97b8-e4e3f10b99da/63b3cde8-987d-4cd9-9306-1955579281d9
To learn more about importing existing cloud resources, see Importing resources.
Package Details
- Repository
- Keycloak pulumi/pulumi-keycloak
- License
- Apache-2.0
- Notes
- This Pulumi package is based on the
keycloakTerraform Provider.
published on Saturday, Feb 21, 2026 by Pulumi
