Search in sources :

Example 1 with RangerClient

use of org.apache.ranger.RangerClient in project ranger by apache.

the class SampleClient method main.

@SuppressWarnings("static-access")
public static void main(String[] args) throws RangerServiceException {
    Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").setPrettyPrinting().create();
    Options options = new Options();
    Option host = OptionBuilder.hasArgs(1).isRequired().withLongOpt("host").withDescription("hostname").create('h');
    Option auth = OptionBuilder.hasArgs(1).isRequired().withLongOpt("authType").withDescription("Authentication Type").create('k');
    Option user = OptionBuilder.hasArgs(1).isRequired().withLongOpt("user").withDescription("username").create('u');
    Option pass = OptionBuilder.hasArgs(1).isRequired().withLongOpt("pass").withDescription("password").create('p');
    Option conf = OptionBuilder.hasArgs(1).withLongOpt("config").withDescription("configuration").create('c');
    options.addOption(host);
    options.addOption(auth);
    options.addOption(user);
    options.addOption(pass);
    options.addOption(conf);
    CommandLineParser parser = new BasicParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    String hostName = cmd.getOptionValue('h');
    String userName = cmd.getOptionValue('u');
    String password = cmd.getOptionValue('p');
    String cfg = cmd.getOptionValue('c');
    String authType = cmd.getOptionValue('k');
    RangerClient rangerClient = new RangerClient(hostName, authType, userName, password, cfg);
    String serviceDefName = "sampleServiceDef";
    String serviceName = "sampleService";
    String policyName = "samplePolicy";
    String roleName = "sampleRole";
    Map<String, String> filter = Collections.emptyMap();
    /*
        Create a new Service Definition
         */
    RangerServiceDef.RangerServiceConfigDef config = new RangerServiceDef.RangerServiceConfigDef();
    config.setItemId(1L);
    config.setName("sampleConfig");
    config.setType("string");
    List<RangerServiceDef.RangerServiceConfigDef> configs = Collections.singletonList(config);
    RangerServiceDef.RangerAccessTypeDef accessType = new RangerServiceDef.RangerAccessTypeDef();
    accessType.setItemId(1L);
    accessType.setName("sampleAccess");
    List<RangerServiceDef.RangerAccessTypeDef> accessTypes = Collections.singletonList(accessType);
    RangerServiceDef.RangerResourceDef resourceDef = new RangerServiceDef.RangerResourceDef();
    resourceDef.setItemId(1L);
    resourceDef.setName("root");
    resourceDef.setType("string");
    List<RangerServiceDef.RangerResourceDef> resourceDefs = Collections.singletonList(resourceDef);
    RangerServiceDef serviceDef = new RangerServiceDef();
    serviceDef.setName(serviceDefName);
    serviceDef.setConfigs(configs);
    serviceDef.setAccessTypes(accessTypes);
    serviceDef.setResources(resourceDefs);
    RangerServiceDef createdServiceDef = rangerClient.createServiceDef(serviceDef);
    LOG.info("New Service Definition created successfully {}", gsonBuilder.toJson(createdServiceDef));
    /*
        Create a new Service
         */
    RangerService service = new RangerService();
    service.setType(serviceDefName);
    service.setName(serviceName);
    RangerService createdService = rangerClient.createService(service);
    LOG.info("New Service created successfully {}", gsonBuilder.toJson(createdService));
    /*
        All Services
         */
    List<RangerService> services = rangerClient.findServices(filter);
    String allServiceNames = "";
    for (RangerService svc : services) {
        allServiceNames = allServiceNames.concat(svc.getName() + " ");
    }
    LOG.info("List of Services : {}", allServiceNames);
    /*
        Policy Management
         */
    /*
        Create a new Policy
         */
    Map<String, RangerPolicy.RangerPolicyResource> resource = Collections.singletonMap("root", new RangerPolicy.RangerPolicyResource(Collections.singletonList("/path/to/sample/resource"), false, false));
    RangerPolicy policy = new RangerPolicy();
    policy.setService(serviceName);
    policy.setName(policyName);
    policy.setResources(resource);
    RangerPolicy createdPolicy = rangerClient.createPolicy(policy);
    LOG.info("New Policy created successfully {}", gsonBuilder.toJson(createdPolicy));
    /*
        Get a policy by name
         */
    RangerPolicy fetchedPolicy = rangerClient.getPolicy(serviceName, policyName);
    LOG.info("Policy: {} fetched {}", policyName, gsonBuilder.toJson(fetchedPolicy));
    /*
        Delete a policy
         */
    rangerClient.deletePolicy(serviceName, policyName);
    LOG.info("Policy {} successfully deleted", policyName);
    /*
        Delete a Service
         */
    rangerClient.deleteService(serviceName);
    LOG.info("Service {} successfully deleted", serviceName);
    /*
        Delete a Service Definition
         */
    rangerClient.deleteServiceDef(serviceDefName);
    LOG.info("Service Definition {} successfully deleted", serviceDefName);
    /*
        Role Management
         */
    /*
        Create a role in Ranger
         */
    RangerRole sampleRole = new RangerRole();
    sampleRole.setName(roleName);
    sampleRole.setDescription("Sample Role");
    sampleRole.setUsers(Collections.singletonList(new RangerRole.RoleMember(null, true)));
    sampleRole = rangerClient.createRole(serviceName, sampleRole);
    LOG.info("New Role successfully created {}", gsonBuilder.toJson(sampleRole));
    /*
        Update a role in Ranger
         */
    sampleRole.setDescription("Updated Sample Role");
    RangerRole updatedRole = rangerClient.updateRole(sampleRole.getId(), sampleRole);
    LOG.info("Role {} successfully updated {}", roleName, gsonBuilder.toJson(updatedRole));
    /*
        Get all roles in Ranger
         */
    List<RangerRole> allRoles = rangerClient.findRoles(filter);
    LOG.info("List of Roles {}", gsonBuilder.toJson(allRoles));
    String allRoleNames = "";
    for (RangerRole role : allRoles) {
        allRoleNames = allRoleNames.concat(role.getName() + " ");
    }
    LOG.info("List of Roles : {}", allRoleNames);
    /*
        Delete a role in Ranger
         */
    rangerClient.deleteRole(roleName, userName, serviceName);
    LOG.info("Role {} successfully deleted", roleName);
}
Also used : Gson(com.google.gson.Gson) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) RangerRole(org.apache.ranger.plugin.model.RangerRole) RangerClient(org.apache.ranger.RangerClient) RangerService(org.apache.ranger.plugin.model.RangerService) GsonBuilder(com.google.gson.GsonBuilder) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef)

Aggregations

Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 RangerClient (org.apache.ranger.RangerClient)1 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)1 RangerRole (org.apache.ranger.plugin.model.RangerRole)1 RangerService (org.apache.ranger.plugin.model.RangerService)1 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)1