Skip to content

MinioAdminClient: add methods (attachPolicies, detachPolicies) #1626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions adminapi/src/main/java/io/minio/admin/AttachPoliciesResp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2022 MinIO, 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 io.minio.admin;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import java.util.Arrays;

/** Represents detachPolicies information. */
@JsonIgnoreProperties(ignoreUnknown = true)
public class AttachPoliciesResp {
@JsonProperty("policiesAttached")
private String[] policiesAttached;

@JsonProperty("updatedAt")
private LocalDateTime updatedAt;

public String[] getPoliciesAttached() {
return policiesAttached;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

@Override
public String toString() {
return "AttachPoliciesResp{"
+ "policiesAttached="
+ Arrays.toString(policiesAttached)
+ ", updatedAt="
+ updatedAt
+ '}';
}
}
51 changes: 51 additions & 0 deletions adminapi/src/main/java/io/minio/admin/DetachPoliciesResp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage,
* (C) 2022 MinIO, 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 io.minio.admin;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import java.util.*;

/** Represents detachPolicies information. */
@JsonIgnoreProperties(ignoreUnknown = true)
public class DetachPoliciesResp {
@JsonProperty("policiesDetached")
private String[] policiesDetached;

@JsonProperty("updatedAt")
private LocalDateTime updatedAt;

public String[] getPoliciesDetached() {
return policiesDetached;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

@Override
public String toString() {
return "DetachPoliciesResp{"
+ "policiesDetached="
+ Arrays.toString(policiesDetached)
+ ", updatedAt="
+ updatedAt
+ '}';
}
}
77 changes: 71 additions & 6 deletions adminapi/src/main/java/io/minio/admin/MinioAdminClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -89,7 +85,10 @@ private enum Command {
UPDATE_SERVICE_ACCOUNT("update-service-account"),
LIST_SERVICE_ACCOUNTS("list-service-accounts"),
DELETE_SERVICE_ACCOUNT("delete-service-account"),
INFO_SERVICE_ACCOUNT("info-service-account");
INFO_SERVICE_ACCOUNT("info-service-account"),
IDP_BUILTIN_POLICY_ATTACH("idp/builtin/policy/attach"),
IDP_BUILTIN_POLICY_DETACH("idp/builtin/policy/detach"),
;
private final String value;

private Command(String value) {
Expand Down Expand Up @@ -843,6 +842,72 @@ public GetServiceAccountInfoResp getServiceAccountInfo(@Nonnull String accessKey
}
}

/**
* Attach or Detach multiple policies to a user or group. <br>
*
* <p>Throws a runtime exception with the error code "XMinioAdminPolicyChangeAlreadyApplied" and
* message "The specified policy change is already in effect." if there is no difference between
* the policies before and after the change.
*
* @param command Command to be executed.
* @param polices Array of policy names to be attached.
* @param user User name.
* @param group Group name.
* @throws NoSuchAlgorithmException Thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws InvalidKeyException Thrown to indicate missing of HMAC SHA-256 library.
* @throws IOException Thrown to indicate I/O error on MinIO REST operation.
*/
private byte[] attachDetachPolicy(
@Nonnull Command command,
@Nonnull List<String> polices,
@Nullable String user,
@Nullable String group)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
InvalidCipherTextException {

if (user == null && group == null) {
throw new IllegalArgumentException("user or group must be provided");
}

Map<String, Object> policyEntity = new HashMap<>();
policyEntity.put("policies", polices);
if (user != null) {
policyEntity.put("user", user);
} else {
policyEntity.put("group", group);
}

Credentials creds = getCredentials();
try (Response response =
execute(
Method.POST,
command,
null,
Crypto.encrypt(OBJECT_MAPPER.writeValueAsBytes(policyEntity), creds.secretKey()))) {
return Crypto.decrypt(response.body().byteStream(), creds.secretKey());
}
}

/** Attach multiple policies to a user or group. */
public AttachPoliciesResp attachPolicy(
@Nonnull List<String> policies, @Nullable String user, @Nullable String group)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
InvalidCipherTextException {
byte[] result =
this.attachDetachPolicy(Command.IDP_BUILTIN_POLICY_ATTACH, policies, user, group);
return OBJECT_MAPPER.readValue(result, AttachPoliciesResp.class);
}

/** Detach multiple policies to a user or group. */
public DetachPoliciesResp detachPolicy(
@Nonnull List<String> policies, @Nullable String user, @Nullable String group)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
InvalidCipherTextException {
byte[] result =
this.attachDetachPolicy(Command.IDP_BUILTIN_POLICY_DETACH, policies, user, group);
return OBJECT_MAPPER.readValue(result, DetachPoliciesResp.class);
}

/**
* Sets HTTP connect, write and read timeouts. A value of 0 means no timeout, otherwise values
* must be between 1 and Integer.MAX_VALUE when converted to milliseconds.
Expand Down