Skip to content

Commit

Permalink
Improve javadoc and code format of sentinel-security-core module
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Aug 16, 2023
1 parent 44ea62b commit 077158e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* 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
*
* https://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 com.alibaba.csp.sentinel.trust;

/**
* @author lwj
* @since 2.0.0
*/
public interface StoreCallback<T> {
public void onUpdate(T newInstance);

void onUpdate(T newInstance);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,14 +23,15 @@
import com.alibaba.csp.sentinel.trust.tls.TlsMode;

/**
* Singleton pattern
* Manager of Sentinel zero-trust cert and rules.
*
* @author lwj
* @since 2.0.0
*/
public class TrustManager {

private static volatile TrustManager instance = null;

private CertPair certPair = null;
private List<StoreCallback<CertPair>> certStoreCallbackList = new ArrayList<>();
private TlsMode tlsMode = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import java.util.Map;


/**
* All auth
* @author lwj
* @since 2.0.0
*/
public class Rules {

Expand All @@ -32,8 +32,8 @@ public class Rules {

private final Map<String, JwtRule> jwtRules;


public Rules(Map<String, AuthRule> allowAuthRules, Map<String, AuthRule> denyAuthRules, Map<String, JwtRule> jwtRules) {
public Rules(Map<String, AuthRule> allowAuthRules, Map<String, AuthRule> denyAuthRules,
Map<String, JwtRule> jwtRules) {
this.allowAuthRules = allowAuthRules;
this.denyAuthRules = denyAuthRules;
this.jwtRules = jwtRules;
Expand All @@ -47,14 +47,13 @@ public Map<String, AuthRule> getDenyAuthRules() {
return denyAuthRules;
}


public Map<String, JwtRule> getJwtRules() {
return jwtRules;
}


@Override
public String toString() {
return "Rules{" + "allowAuthRules=" + allowAuthRules + ", denyAuthRules=" + denyAuthRules + ", jwtRules=" + jwtRules + '}';
return "Rules{" + "allowAuthRules=" + allowAuthRules + ", denyAuthRules=" + denyAuthRules + ", jwtRules="
+ jwtRules + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @since 2.0.0
*/
public class AuthCondition {

/**
* authType , depending on the judgment, request values in different places
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
package com.alibaba.csp.sentinel.trust.auth.rule;

/**
* All types of auth validation.
* <p>
* The rules are detailed in
* https://istio.io/latest/docs/reference/config/security/authorization-policy/#Rule-From
* and
* https://istio.io/latest/docs/reference/config/security/conditions/
* <p>All types of auth validation.</p>
*
* @author lwj
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Objects;

/**
* Certificate
* Certificate.
*
* @author lwj
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.util.Objects;

/**
* Tlsmode:
* <p>
* 1)DISABLE:Connection is not tunneled.
* 2)PERMISSIVE:Connection can be either plaintext or mTLS tunnel.
* 3)STRICT:Connection is an mTLS tunnel (TLS with client cert must be presented).
* TLS mode:
* <ul>
* <li>DISABLE:Connection is not tunneled.</li>
* <li>PERMISSIVE:Connection can be either plaintext or mTLS tunnel.</li>
* <li>STRICT:Connection is an mTLS tunnel (TLS with client cert must be presented).</li>
* </ul>
*
* @author lwj
* @since 2.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.MalformedClaimException;


/**
* A validator that verifies whether the request complies with Rules
* A validator that verifies whether the request complies with Rules.
*
* @author lwj
* @since 2.0.0
Expand All @@ -47,7 +46,8 @@ private AuthValidator() {

/**
* The specific rules are:
* (1) If there is a JWT rule matching the request, the request is matched. If the matching result is rejected, the request is rejected.
* (1) If there is a JWT rule matching the request, the request is matched. If the matching result is rejected, the
* request is rejected.
* (2) If any DENY policy matches the request, the request is denied.
* (3) ALLOW the request if the workload does not have an Allow policy.
* (4) ALLOW any Allow policy if it matches the request.
Expand All @@ -64,9 +64,7 @@ public static boolean validate(UnifiedHttpRequest request, Rules rules) {
return true;
}

/**
* The first step is to extract the corresponding token
*/
// The first step is to extract the corresponding token.
for (JwtRule jwtRule : rules.getJwtRules().values()) {
String token = JwtUtil.getTokenFromJwtRule(request.getParams(), request.getHeaders(), jwtRule);
if (!StringUtil.isEmpty(token)) {
Expand Down Expand Up @@ -113,7 +111,7 @@ public static boolean validateJwtRule(JwtRule jwtRule, JwtClaims jwtClaims) {
}
try {
if (!StringUtil.isBlank(jwtRule.getIssuer())
&& !jwtRule.getIssuer().equals(jwtClaims.getIssuer())) {
&& !jwtRule.getIssuer().equals(jwtClaims.getIssuer())) {
return false;
}

Expand All @@ -134,7 +132,8 @@ public static boolean validateJwtRule(JwtRule jwtRule, JwtClaims jwtClaims) {

}
//Guarantee not expired
if (null == jwtClaims.getExpirationTime() || jwtClaims.getExpirationTime().getValueInMillis() <= System.currentTimeMillis()) {
if (null == jwtClaims.getExpirationTime()
|| jwtClaims.getExpirationTime().getValueInMillis() <= System.currentTimeMillis()) {
return false;
}
return true;
Expand All @@ -144,7 +143,6 @@ public static boolean validateJwtRule(JwtRule jwtRule, JwtClaims jwtClaims) {
return false;
}


public static boolean validateRule(AuthRule authRule, UnifiedHttpRequest request) {
if (authRule.isLeaf()) {
return validateLeafRule(authRule, request);
Expand Down Expand Up @@ -261,5 +259,4 @@ public static boolean validateLeafRule(AuthRule rule, UnifiedHttpRequest request
return false;
}


}

0 comments on commit 077158e

Please sign in to comment.