Skip to content

Commit

Permalink
Support API V2 : Get / Create / Delete Permission targets
Browse files Browse the repository at this point in the history
  • Loading branch information
smoneuse committed Oct 9, 2023
1 parent 777f4c9 commit 0554c85
Show file tree
Hide file tree
Showing 19 changed files with 773 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/src/main/java/org/jfrog/artifactory/client/Security.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.Group;
import org.jfrog.artifactory.client.model.PermissionTarget;
import org.jfrog.artifactory.client.model.PermissionTargetV2;
import org.jfrog.artifactory.client.model.User;
import org.jfrog.artifactory.client.model.builder.SecurityBuilders;

Expand Down Expand Up @@ -30,6 +31,8 @@ public interface Security {

PermissionTarget permissionTarget(String name);

PermissionTargetV2 permissionTargetV2(String name);

List<String> permissionTargets();

void createOrUpdate(User user);
Expand All @@ -38,17 +41,25 @@ public interface Security {

void createOrReplacePermissionTarget(PermissionTarget permissionTarget);

void createOrReplacePermissionTargetV2(PermissionTargetV2 permissionTarget);

String deleteUser(String name);

String deleteGroup(String name);

String deletePermissionTarget(String name);

String deletePermissionTargetV2(String name);

String getSecurityApi();

String getSecurityV2Api();

String getSecurityUsersApi();

String getSecurityPermissionsApi();

String getSecurityPermissionsV2Api();

String getSecurityUserGroupsApi();
}
15 changes: 15 additions & 0 deletions api/src/main/java/org/jfrog/artifactory/client/model/Actions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jfrog.artifactory.client.model;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface Actions {
Map<String, Set<PrivilegeV2>> getUsers();
Map<String, Set<PrivilegeV2>> getGroups();

boolean isUserAllowedTo(String user, PrivilegeV2 privilege);
boolean isGroupAllowedTo(String group, PrivilegeV2 privilege);
@Override
boolean equals(Object o);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jfrog.artifactory.client.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface PermissionTargetV2 {
String getName();
PermissionV2 getRepo();
PermissionV2 getBuild();
PermissionV2 getReleaseBundle();
@Override
boolean equals(Object o);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jfrog.artifactory.client.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;
import java.util.Map;
@JsonIgnoreProperties(ignoreUnknown = true)
public interface PermissionV2 {
List<String> getIncludePatterns();
List<String> getExcludePatterns();
List<String> getRepositories();
Actions getActions();
@Override
boolean equals(Object o);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jfrog.artifactory.client.model;

import com.fasterxml.jackson.annotation.JsonValue;

public enum PrivilegeV2 {
READ("read"),WRITE("write"),ANNOTATE("annotate"),DELETE("delete"),MANAGE("manage"),MANAGED_XRAY_META("managedXrayMeta"),DISTRIBUTE("distribute");

@JsonValue
private String privilege;

PrivilegeV2(String privilege) {
this.privilege = privilege;
}

public String getPrivilege() {
return privilege;
}

public static PrivilegeV2 fromPrivilege(String privilege){
for (PrivilegeV2 privilegeV2 : values()) {
if (privilegeV2.privilege.equals(privilege)) {
return privilegeV2;
}
}
throw new IllegalArgumentException("No Privilege for "+privilege+" found.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.jfrog.artifactory.client.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.Actions;
import org.jfrog.artifactory.client.model.PrivilegeV2;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface ActionsBuilder {

ActionsBuilder addUser(String user, PrivilegeV2... privileges);
ActionsBuilder addGroup(String group, PrivilegeV2... privileges);
Actions build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jfrog.artifactory.client.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.PermissionTargetV2;
import org.jfrog.artifactory.client.model.PermissionV2;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface PermissionTargetV2Builder {

PermissionTargetV2Builder name(String name);
PermissionTargetV2Builder repo(PermissionV2 repo);
PermissionTargetV2Builder build(PermissionV2 build);
PermissionTargetV2Builder releaseBundle(PermissionV2 releaseBundle);
PermissionTargetV2 build();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jfrog.artifactory.client.model.builder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.jfrog.artifactory.client.model.Actions;
import org.jfrog.artifactory.client.model.PermissionV2;

@JsonIgnoreProperties(ignoreUnknown = true)
public interface PermissionV2Builder {

PermissionV2Builder includePatterns(String... includePatterns);
PermissionV2Builder excludePatterns(String... excludePatterns);
PermissionV2Builder repositories(String... repositories);
PermissionV2Builder actions(Actions actions);
PermissionV2 build();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public interface SecurityBuilders {
PrincipalsBuilder principalsBuilder();

PrincipalBuilder principalBuilder();

PermissionTargetV2Builder permissionTargetV2Builder();

PermissionV2Builder permissionV2Builder();

ActionsBuilder actionsBuilder();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.jfrog.artifactory.client.impl

import org.apache.commons.lang3.StringUtils
import org.apache.http.entity.ContentType
import org.jfrog.artifactory.client.Security
import org.jfrog.artifactory.client.impl.util.Util
import org.jfrog.artifactory.client.model.Group
import org.jfrog.artifactory.client.model.PermissionTarget
import org.jfrog.artifactory.client.model.PermissionTargetV2
import org.jfrog.artifactory.client.model.User
import org.jfrog.artifactory.client.model.builder.SecurityBuilders
import org.jfrog.artifactory.client.model.impl.PermissionTargetV2Impl
import org.jfrog.artifactory.client.model.impl.SecurityBuildersImpl
import org.jfrog.artifactory.client.model.impl.UserBuilderImpl
import org.jfrog.artifactory.client.model.impl.GroupImpl
Expand Down Expand Up @@ -60,6 +63,12 @@ class SecurityImpl implements Security {
return artifactory.get("${getSecurityPermissionsApi()}/$name", PermissionTargetImpl, PermissionTarget)
}

@Override
PermissionTargetV2 permissionTargetV2(String name){
name = Util.encodeParams(name);
return artifactory.get("${getSecurityPermissionsV2Api()}/$name", PermissionTargetV2Impl, PermissionTargetV2)
}

@Override
List<String> groupNames() {
GroupImpl[] groups = artifactory.get("${getSecurityUserGroupsApi()}", GroupImpl[], null)
Expand Down Expand Up @@ -99,6 +108,25 @@ class SecurityImpl implements Security {
new HashMap<String, String>(), null, -1, String, null)
}

@Override
void createOrReplacePermissionTargetV2(PermissionTargetV2 permissionTarget) {
if(permissionTarget == null || StringUtils.isBlank(permissionTarget.getName())){
throw new IllegalArgumentException("Permission target name is required")
}
if (permissionTarget.getRepo() == null || permissionTarget.getRepo().getRepositories()==null || permissionTarget.getRepo().getRepositories().isEmpty()) {
throw new UnsupportedOperationException("At least 1 repository is required in permission target (could be 'ANY', 'ANY LOCAL', 'ANY REMOTE')")
}
if(permissionTarget.getBuild() != null && permissionTarget.getBuild().getRepositories() !=null){
List<String> buildRepositories = permissionTarget.getBuild().getRepositories();
if(buildRepositories.size() !=1 || !buildRepositories.contains("artifactory-build-info")){
throw new UnsupportedOperationException("Only 'artifactory-build-info' repository is supported for build permission target")
}
}
String name = Util.encodeParams(permissionTarget.getName());
artifactory.put("${getSecurityPermissionsV2Api()}/$name", ContentType.APPLICATION_JSON, Util.getStringFromObject(permissionTarget),
new HashMap<String, String>(), null, -1, String, null)
}

@Override
String deleteUser(String name) {
name = Util.encodeParams(name);
Expand All @@ -117,11 +145,22 @@ class SecurityImpl implements Security {
artifactory.delete("${getSecurityPermissionsApi()}/$name")
}

@Override
String deletePermissionTargetV2(String name) {
name = Util.encodeParams(name);
artifactory.delete("${getSecurityPermissionsV2Api()}/$name")
}

@Override
String getSecurityApi() {
return baseApiPath + "/security/";
}

@Override
String getSecurityV2Api() {
return baseApiPath + "/v2/security/";
}

@Override
String getSecurityUsersApi() {
return getSecurityApi() + "users";
Expand All @@ -132,6 +171,11 @@ class SecurityImpl implements Security {
return getSecurityApi() + "permissions";
}

@Override
String getSecurityPermissionsV2Api() {
return getSecurityV2Api() + "permissions";
}

@Override
String getSecurityUserGroupsApi() {
return getSecurityApi() + "groups";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jfrog.artifactory.client.model.impl;

import org.jfrog.artifactory.client.model.Actions;
import org.jfrog.artifactory.client.model.PrivilegeV2;
import org.jfrog.artifactory.client.model.builder.ActionsBuilder;

import java.util.*;

public class ActionsBuilderImpl implements ActionsBuilder {
private Map<String, Set<PrivilegeV2>> usersGrantedActions;
private Map<String, Set<PrivilegeV2>> groupsGrantedActions;

public ActionsBuilderImpl() {
this.usersGrantedActions= new HashMap<>();
this.groupsGrantedActions = new HashMap<>();
}
@Override
public ActionsBuilder addUser(String user, PrivilegeV2... privileges) {
Set<PrivilegeV2> userPrivileges = new HashSet<>(Arrays.asList(privileges));
usersGrantedActions.put(user, userPrivileges);
return this;
}

@Override
public ActionsBuilder addGroup(String group, PrivilegeV2... privileges) {
Set<PrivilegeV2> groupPrivileges = new HashSet<>(Arrays.asList(privileges));
groupsGrantedActions.put(group, groupPrivileges);
return this;
}

@Override
public Actions build() {
return new ActionsImpl(usersGrantedActions, groupsGrantedActions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jfrog.artifactory.client.model.impl;

import org.apache.commons.lang3.StringUtils;
import org.jfrog.artifactory.client.model.Actions;
import org.jfrog.artifactory.client.model.PrivilegeV2;

import java.util.*;

public class ActionsImpl implements Actions {

private Map<String, Set<PrivilegeV2>> users;

private Map<String, Set<PrivilegeV2>> groups;

public ActionsImpl() {
super();
this.users=new HashMap<>();
this.groups=new HashMap<>();
}

public ActionsImpl(Map<String, Set<PrivilegeV2>> users, Map<String, Set<PrivilegeV2>> groups) {
this.users = Optional.ofNullable(users).orElse(Collections.emptyMap());
this.groups = Optional.ofNullable(groups).orElse(Collections.emptyMap());
}

@Override
public boolean isUserAllowedTo(String user, PrivilegeV2 privilege) {
if(StringUtils.isBlank(user) || privilege == null) {
return false;
}
return users.containsKey(user) && users.get(user).contains(privilege);
}

@Override
public boolean isGroupAllowedTo(String group, PrivilegeV2 privilege) {
if(StringUtils.isBlank(group) || privilege == null) {
return false;
}
return groups.containsKey(group) && groups.get(group).contains(privilege);
}

@Override
public Map<String, Set<PrivilegeV2>> getUsers() {
return users;
}

@Override
public Map<String, Set<PrivilegeV2>> getGroups() {
return groups;
}

public void setUsers(Map<String, Set<PrivilegeV2>> users) {
this.users = users;
}

public void setGroups(Map<String, Set<PrivilegeV2>> groups) {
this.groups = groups;
}

@Override
public boolean equals(Object obj) {
if(!(obj instanceof Actions)) {
return false;
}
Actions other = (Actions) obj;
boolean areEquals = (users==null && other.getUsers()==null) ||
(users==null && other.getUsers().isEmpty()) ||
(users.isEmpty() && other.getUsers()==null) ||
(users!=null && users.equals(other.getUsers()));
areEquals &= (groups==null && other.getGroups()==null) ||
(groups==null && other.getGroups().isEmpty()) ||
(groups.isEmpty() && other.getGroups()==null) ||
(groups!=null && groups.equals(other.getGroups()));
return areEquals;
}
}
Loading

0 comments on commit 0554c85

Please sign in to comment.