All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.test.security.common.elytron.DistributedRealm Maven / Gradle / Ivy

/*
 * Copyright 2020 Red Hat, Inc.
 *
 * 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 org.wildfly.test.security.common.elytron;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.test.integration.management.util.CLIWrapper;
import org.jboss.as.test.integration.security.common.Utils;
import org.jboss.dmr.ModelNode;

/**
 * A {@link ConfigurableElement} to define the distributed-realm resource within the Elytron subsystem.
 *
 * @author Ondrej Kotek
 */
public class DistributedRealm implements SecurityRealm {

    private final PathAddress address;
    private final String name;
    private final String[] realms;

    DistributedRealm(final String name, final String[] realms) {
        this.name = name;
        this.address = PathAddress.pathAddress(PathElement.pathElement("subsystem", "elytron"), PathElement.pathElement("distributed-realm", name));
        this.realms = realms;
    }

    @Override
    public String getName() {
        return name;
    }

    public ModelNode getAddOperation() {
        ModelNode addOperation = Util.createAddOperation(address);
        if (realms != null) {
            ModelNode realmsList = addOperation.get("realms");
            for (String realmName : realms) {
                realmsList.add(realmName);
            }
        }

        return addOperation;
    }

    public ModelNode getRemoveOperation() {
        return Util.createRemoveOperation(address);
    }

    @Override
    public void create(ModelControllerClient client, CLIWrapper cli) throws Exception {
        Utils.applyUpdate(getAddOperation(), client);
    }

    @Override
    public void remove(ModelControllerClient client, CLIWrapper cli) throws Exception {
        Utils.applyUpdate(getRemoveOperation(), client);
    }

    public static Builder builder(final String name) {
        return new Builder(name);
    }

    public static class Builder {

        private final String name;
        private String[] realms;

        Builder(final String name) {
            this.name = name;
        }

        public Builder withRealms(final String... realms) {
            this.realms = realms;

            return this;
        }

        public SecurityRealm build() {
            return new DistributedRealm(name, realms);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy