7 Replies Latest reply on Oct 22, 2015 4:07 AM by J Prasanna Venkatesan

    Please provide a sample ModelControllerClient class for add() attribute

    J Prasanna Venkatesan Newbie

      Hi,

       

      Please provide a sample ModelControllerClient class for add() attribute

       

      I tried a code but it didn't work


      Thanks & Regards,

      J Prasanna Venkatesan

        • 1. Re: Please provide a sample ModelControllerClient class for add() attribute
          Tomaz Cerar Master

          What exactly are you looking for?

          add for what? what attribute? where?

          • 2. Re: Please provide a sample ModelControllerClient class for add() attribute
            J Prasanna Venkatesan Newbie

            I want to add two login modules of same type (i.e. their code should be same) using ModelControllerClient API

             

            More info is here Not able to create two LDAP login module with same code in Wildfly8.2.0

            • 3. Re: Please provide a sample ModelControllerClient class for add() attribute
              J Prasanna Venkatesan Newbie

              From java code using ModelControllerClient class, I am not able to send the /login-module=Ldap (I have bold the text for your convenience) . I gone through various issues/discussion and jboss experts call /login-module=Ldap as 'sub resource'. Whether my understanding is right? If yes, please let me know how to include a sub resource like /login-module=Ldap in the ModelControllerClient  based java code

               

              /subsystem=security/security-domain=SourceForge/authentication=classic/login-module=Ldap:add(code=org.jboss.security.auth.spi.LdapLoginModule, flag=sufficient, module-options=[ "java.naming.provider.url" => "ldap://ldaphost.jboss.org:1389", "java.naming.security.authentication" => "simple", "principalDNPrefix" => "uid=", "principalDNSuffix" => ",ou=People,o=jboss.org", "allowEmptyPasswords" => "false", "java.naming.factory.initial" => "com.sun.jndi.ldap.LdapCtxFactory", "throwValidateError" => "true" ])

              • 4. Re: Please provide a sample ModelControllerClient class for add() attribute
                Tomaz Cerar Master

                can you show your complete code sniplet(with call to modelcontroller client's .execute() method) that doesn't work properly

                 

                as syntax you are posting is only used in CLI, going via model controller client you need to use pure DMR(ModelNode) approach

                which is completely generic and allows you to call any operation however many times you want.

                • 5. Re: Please provide a sample ModelControllerClient class for add() attribute
                  J Prasanna Venkatesan Newbie

                  Please find the code here

                   

                  public boolean writeToSA(ArrayList<AuthenticationProfile> profiles, boolean replace, boolean apply) throws Exception{

                       try {

                                      String host = AuthManagerProperties.getAsString( "jboss.host");

                                      Integer port = AuthManagerProperties.getAsInt( "jboss.port" );

                                      ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName(host), port);

                   

                                      int count = 1;

                                      operation = "add";

                   

                                      for( AuthenticationProfile profile : profiles){

                                              JSONArray tempArray = new JSONArray( profile.getLoginModule().getSAReadyJSON());

                                              JSONArray jsonArray = new JSONArray();

                                              jsonArray.put( tempArray.getJSONObject(0) );

                   

                                              inputString = jsonArray.toString();

                                              ModelNode op = new ModelNode();

                                              op.get("operation").set(operation);    

                                              op.get("code").set(profile.getLoginModule().getCode());

                                              op.get("flag").set(profile.getLoginModule().getFlag());

                   

                                              LinkedHashMap moduleHashMap = profile.getLoginModule().getOptionsMap();

                                              String value = "";

                                              int index = 0;

                                              for (Object k : moduleHashMap.keySet()) {

                                                  String key = (String) k;

                                                  if(index == 0){

                                                          value = "\""+key+"\" => \""+moduleHashMap.get(key)+"\"";

                                                  } else {

                                                          value = value + ", \""+key+"\" => \""+moduleHashMap.get(key)+"\"";

                                                  }

                                                  index++;

                                              }

                                              value = "[ " + value + " ]";

                                              op.get("module-options").set(value);

                                              op.get("module").set(profile.getLoginModule().getCode()+count);

                   

                                              ModelNode address = op.get("address");

                                              address.add("subsystem", "security");

                                              address.add("security-domain", securityDomain);

                                              address.add("authentication", "classic");

                                              count++;

                   

                                              op.get("recursive").set(false);

                                              op.get("operations").set(false);

                   

                                              if ( apply ){

                                                      op.get("operation-headers", "allow-resource-service-restart").set(true);

                                              }

                   

                                              ModelNode returnVal = client.execute(op);

                   

                                              String outcome = returnVal.asString();

                                              System.out.println("outcomeoutcomeoutcomeoutcomeoutcome :"+ outcome);

                                              String failureDesc = returnVal.get("failure-description").toString();

                                              System.out.println("failureDescfailureDescfailureDesc: "+ failureDesc);

                                      }

                                      client.close();

                              } catch (Exception e) {

                                      e.printStackTrace();

                                      throw new Exception( e.getCause());

                              }

                              return true;

                      }

                  • 6. Re: Please provide a sample ModelControllerClient class for add() attribute
                    Tomaz Cerar Master

                    This looks quite messy, but i think this is where your problem resides:

                     

                                                value = "[ " + value + " ]";

                                                op.get("module-options").set(value);

                                                op.get("module").set(profile.getLoginModule().getCode()+count);

                     

                                                ModelNode address = op.get("address");

                                                address.add("subsystem", "security");

                                                address.add("security-domain", securityDomain);

                                                address.add("authentication", "classic");

                                                count++;

                     

                                                op.get("recursive").set(false);

                                                op.get("operations").set(false);

                     

                                                if ( apply ){

                                                        op.get("operation-headers", "allow-resource-service-restart").set(true);

                                                }

                     

                       

                    First, recursive & operations attributes are not used for add operations, they are only part of read-resource and similar.

                    anyhow, your address is not complete.

                     

                    ModelNode address = op.get("address");

                    address.add("subsystem", "security");

                    address.add("security-domain", securityDomain);

                    address.add("authentication", "classic");

                    address.add("login-module", <name-of-login-module>)

                    this is what you are missing, and if you are adding more login modules, just use different name here.

                    • 7. Re: Please provide a sample ModelControllerClient class for add() attribute
                      J Prasanna Venkatesan Newbie

                      Thanks for your response.

                       

                      address.add("login-module", <name-of-login-module>)

                       

                      I tried adding login-module in the address. I got IllegalArgumentException in server.log That is why I removed it.