paf91 commented on code in PR #10266: URL: https://github.com/apache/ozone/pull/10266#discussion_r3295500915
########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/security/oidc/OidcConfig.java: ########## @@ -0,0 +1,395 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hadoop.ozone.security.oidc; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ALLOW_INSECURE_HTTP_FOR_TESTS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ALLOW_INSECURE_HTTP_FOR_TESTS_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_AUDIENCE; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_AUDIENCE_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_CLOCK_SKEW; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_CLOCK_SKEW_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ENABLED; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ENABLED_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_GROUPS_CLAIM; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_GROUPS_CLAIM_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ISSUER_URI; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ISSUER_URI_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_CONNECT_TIMEOUT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_CONNECT_TIMEOUT_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_READ_TIMEOUT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_READ_TIMEOUT_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_REFRESH_INTERVAL; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_REFRESH_INTERVAL_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_SIZE_LIMIT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_SIZE_LIMIT_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_URI; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_JWKS_URI_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_REQUIRE_HTTPS; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_REQUIRE_HTTPS_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ROLES_CLAIM; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_ROLES_CLAIM_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_SUBJECT_CLAIM; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_SUBJECT_CLAIM_DEFAULT; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_USERNAME_CLAIM; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_STS_WEB_IDENTITY_USERNAME_CLAIM_DEFAULT; + +import java.net.URI; +import java.net.URISyntaxException; +import java.time.Duration; +import java.util.concurrent.TimeUnit; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.hdds.conf.StorageUnit; + +/** + * Configuration for the experimental OIDC identity provider. + */ +public final class OidcConfig { + + private final boolean enabled; + private final String issuerUri; + private final String jwksUri; + private final String audience; + private final String usernameClaim; + private final String subjectClaim; + private final String groupsClaim; + private final String rolesClaim; + private final Duration clockSkew; + private final Duration jwksRefreshInterval; + private final Duration jwksConnectTimeout; + private final Duration jwksReadTimeout; + private final int jwksSizeLimit; + private final boolean requireHttps; + private final boolean allowInsecureHttpForTests; + + private OidcConfig(Builder builder) { + this.enabled = builder.enabled; + this.issuerUri = trimToEmpty(builder.issuerUri); + this.jwksUri = trimToEmpty(builder.jwksUri); + this.audience = trimToEmpty(builder.audience); + this.usernameClaim = requireNonBlank(builder.usernameClaim, + OZONE_STS_WEB_IDENTITY_USERNAME_CLAIM); + this.subjectClaim = requireNonBlank(builder.subjectClaim, + OZONE_STS_WEB_IDENTITY_SUBJECT_CLAIM); + this.groupsClaim = requireNonBlank(builder.groupsClaim, + OZONE_STS_WEB_IDENTITY_GROUPS_CLAIM); + this.rolesClaim = requireNonBlank(builder.rolesClaim, + OZONE_STS_WEB_IDENTITY_ROLES_CLAIM); + this.clockSkew = requireNonNegative(builder.clockSkew, + OZONE_STS_WEB_IDENTITY_CLOCK_SKEW); + this.jwksRefreshInterval = requireNonNegative(builder.jwksRefreshInterval, + OZONE_STS_WEB_IDENTITY_JWKS_REFRESH_INTERVAL); + this.jwksConnectTimeout = requirePositive(builder.jwksConnectTimeout, + OZONE_STS_WEB_IDENTITY_JWKS_CONNECT_TIMEOUT); + this.jwksReadTimeout = requirePositive(builder.jwksReadTimeout, + OZONE_STS_WEB_IDENTITY_JWKS_READ_TIMEOUT); + this.jwksSizeLimit = requirePositive(builder.jwksSizeLimit, + OZONE_STS_WEB_IDENTITY_JWKS_SIZE_LIMIT); + this.requireHttps = builder.requireHttps; + this.allowInsecureHttpForTests = builder.allowInsecureHttpForTests; + } + + public static OidcConfig from(ConfigurationSource conf) { + OidcConfig config = newBuilder() + .setEnabled(conf.getBoolean(OZONE_STS_WEB_IDENTITY_ENABLED, + OZONE_STS_WEB_IDENTITY_ENABLED_DEFAULT)) + .setIssuerUri(conf.getTrimmed(OZONE_STS_WEB_IDENTITY_ISSUER_URI, + OZONE_STS_WEB_IDENTITY_ISSUER_URI_DEFAULT)) + .setJwksUri(conf.getTrimmed(OZONE_STS_WEB_IDENTITY_JWKS_URI, + OZONE_STS_WEB_IDENTITY_JWKS_URI_DEFAULT)) + .setAudience(conf.getTrimmed(OZONE_STS_WEB_IDENTITY_AUDIENCE, + OZONE_STS_WEB_IDENTITY_AUDIENCE_DEFAULT)) + .setUsernameClaim(conf.getTrimmed( + OZONE_STS_WEB_IDENTITY_USERNAME_CLAIM, + OZONE_STS_WEB_IDENTITY_USERNAME_CLAIM_DEFAULT)) + .setSubjectClaim(conf.getTrimmed( + OZONE_STS_WEB_IDENTITY_SUBJECT_CLAIM, + OZONE_STS_WEB_IDENTITY_SUBJECT_CLAIM_DEFAULT)) + .setGroupsClaim(conf.getTrimmed(OZONE_STS_WEB_IDENTITY_GROUPS_CLAIM, + OZONE_STS_WEB_IDENTITY_GROUPS_CLAIM_DEFAULT)) + .setRolesClaim(conf.getTrimmed(OZONE_STS_WEB_IDENTITY_ROLES_CLAIM, + OZONE_STS_WEB_IDENTITY_ROLES_CLAIM_DEFAULT)) + .setClockSkew(duration(conf, OZONE_STS_WEB_IDENTITY_CLOCK_SKEW, + OZONE_STS_WEB_IDENTITY_CLOCK_SKEW_DEFAULT)) + .setJwksRefreshInterval(duration(conf, + OZONE_STS_WEB_IDENTITY_JWKS_REFRESH_INTERVAL, + OZONE_STS_WEB_IDENTITY_JWKS_REFRESH_INTERVAL_DEFAULT)) + .setJwksConnectTimeout(duration(conf, + OZONE_STS_WEB_IDENTITY_JWKS_CONNECT_TIMEOUT, + OZONE_STS_WEB_IDENTITY_JWKS_CONNECT_TIMEOUT_DEFAULT)) + .setJwksReadTimeout(duration(conf, + OZONE_STS_WEB_IDENTITY_JWKS_READ_TIMEOUT, + OZONE_STS_WEB_IDENTITY_JWKS_READ_TIMEOUT_DEFAULT)) + .setJwksSizeLimit(storageSize(conf, + OZONE_STS_WEB_IDENTITY_JWKS_SIZE_LIMIT, + OZONE_STS_WEB_IDENTITY_JWKS_SIZE_LIMIT_DEFAULT)) + .setRequireHttps(conf.getBoolean(OZONE_STS_WEB_IDENTITY_REQUIRE_HTTPS, + OZONE_STS_WEB_IDENTITY_REQUIRE_HTTPS_DEFAULT)) + .setAllowInsecureHttpForTests(conf.getBoolean( + OZONE_STS_WEB_IDENTITY_ALLOW_INSECURE_HTTP_FOR_TESTS, + OZONE_STS_WEB_IDENTITY_ALLOW_INSECURE_HTTP_FOR_TESTS_DEFAULT)) + .build(); + if (config.isEnabled()) { + config.validateForProvider(); + } + return config; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public boolean isEnabled() { + return enabled; + } + + public String getIssuerUri() { + return issuerUri; + } + + public String getJwksUri() { + return jwksUri; + } + + public String getAudience() { + return audience; + } + + public String getUsernameClaim() { + return usernameClaim; + } + + public String getSubjectClaim() { + return subjectClaim; + } + + public String getGroupsClaim() { + return groupsClaim; + } + + public String getRolesClaim() { + return rolesClaim; + } + + public Duration getClockSkew() { + return clockSkew; + } + + public Duration getJwksRefreshInterval() { + return jwksRefreshInterval; + } + + public Duration getJwksConnectTimeout() { + return jwksConnectTimeout; + } + + public Duration getJwksReadTimeout() { + return jwksReadTimeout; + } + + public int getJwksSizeLimit() { + return jwksSizeLimit; + } + + public boolean isRequireHttps() { + return requireHttps; + } + + public boolean isAllowInsecureHttpForTests() { + return allowInsecureHttpForTests; + } + + void validateForProvider() { + requireNonBlank(issuerUri, OZONE_STS_WEB_IDENTITY_ISSUER_URI); + requireNonBlank(audience, OZONE_STS_WEB_IDENTITY_AUDIENCE); + + if (requireHttps && !allowInsecureHttpForTests) { Review Comment: Thanks, removed `ozone.sts.web.identity.allow.insecure.http.for.tests` and use `ozone.sts.web.identity.require.https=false` explicitly in tests/local Keycloak setup. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
