|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.extension.auth.opa; |
| 20 | + |
| 21 | +import static com.google.common.base.Preconditions.checkArgument; |
| 22 | + |
| 23 | +import com.google.common.base.Strings; |
| 24 | +import io.smallrye.config.ConfigMapping; |
| 25 | +import io.smallrye.config.WithDefault; |
| 26 | +import java.net.URI; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.Optional; |
| 30 | +import org.apache.polaris.immutables.PolarisImmutable; |
| 31 | + |
| 32 | +/** |
| 33 | + * Configuration for OPA (Open Policy Agent) authorization. |
| 34 | + * |
| 35 | + * <p><strong>Beta Feature:</strong> OPA authorization is currently in Beta and is not a stable |
| 36 | + * release. It may undergo breaking changes in future versions. Use with caution in production |
| 37 | + * environments. |
| 38 | + */ |
| 39 | +@PolarisImmutable |
| 40 | +@ConfigMapping(prefix = "polaris.authorization.opa") |
| 41 | +public interface OpaAuthorizationConfig { |
| 42 | + |
| 43 | + /** Authentication types supported by OPA authorization */ |
| 44 | + enum AuthenticationType { |
| 45 | + NONE("none"), |
| 46 | + BEARER("bearer"); |
| 47 | + |
| 48 | + private final String value; |
| 49 | + |
| 50 | + AuthenticationType(String value) { |
| 51 | + this.value = value; |
| 52 | + } |
| 53 | + |
| 54 | + public String getValue() { |
| 55 | + return value; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Optional<URI> policyUri(); |
| 60 | + |
| 61 | + AuthenticationConfig auth(); |
| 62 | + |
| 63 | + HttpConfig http(); |
| 64 | + |
| 65 | + /** Validates the complete OPA configuration */ |
| 66 | + default void validate() { |
| 67 | + checkArgument( |
| 68 | + policyUri().isPresent(), "polaris.authorization.opa.policy-uri must be configured"); |
| 69 | + |
| 70 | + URI uri = policyUri().get(); |
| 71 | + String scheme = uri.getScheme(); |
| 72 | + checkArgument( |
| 73 | + "http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme), |
| 74 | + "polaris.authorization.opa.policy-uri must use http or https scheme, but got: " + scheme); |
| 75 | + |
| 76 | + auth().validate(); |
| 77 | + } |
| 78 | + |
| 79 | + /** HTTP client configuration for OPA communication. */ |
| 80 | + @PolarisImmutable |
| 81 | + interface HttpConfig { |
| 82 | + @WithDefault("PT2S") |
| 83 | + Duration timeout(); |
| 84 | + |
| 85 | + @WithDefault("true") |
| 86 | + boolean verifySsl(); |
| 87 | + |
| 88 | + Optional<Path> trustStorePath(); |
| 89 | + |
| 90 | + Optional<String> trustStorePassword(); |
| 91 | + } |
| 92 | + |
| 93 | + /** Authentication configuration for OPA communication. */ |
| 94 | + @PolarisImmutable |
| 95 | + interface AuthenticationConfig { |
| 96 | + /** Type of authentication */ |
| 97 | + @WithDefault("none") |
| 98 | + AuthenticationType type(); |
| 99 | + |
| 100 | + /** Bearer token authentication configuration */ |
| 101 | + Optional<BearerTokenConfig> bearer(); |
| 102 | + |
| 103 | + default void validate() { |
| 104 | + switch (type()) { |
| 105 | + case BEARER: |
| 106 | + checkArgument( |
| 107 | + bearer().isPresent(), "Bearer configuration is required when type is 'bearer'"); |
| 108 | + bearer().get().validate(); |
| 109 | + break; |
| 110 | + case NONE: |
| 111 | + // No authentication - nothing to validate |
| 112 | + break; |
| 113 | + default: |
| 114 | + throw new IllegalArgumentException( |
| 115 | + "Invalid authentication type: " + type() + ". Supported types: 'bearer', 'none'"); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @PolarisImmutable |
| 121 | + interface BearerTokenConfig { |
| 122 | + /** Static bearer token configuration */ |
| 123 | + Optional<StaticTokenConfig> staticToken(); |
| 124 | + |
| 125 | + /** File-based bearer token configuration */ |
| 126 | + Optional<FileBasedConfig> fileBased(); |
| 127 | + |
| 128 | + default void validate() { |
| 129 | + // Ensure exactly one bearer token configuration is present (mutually exclusive) |
| 130 | + checkArgument( |
| 131 | + staticToken().isPresent() ^ fileBased().isPresent(), |
| 132 | + "Exactly one of 'static-token' or 'file-based' bearer token configuration must be specified"); |
| 133 | + |
| 134 | + // Validate the present configuration |
| 135 | + if (staticToken().isPresent()) { |
| 136 | + staticToken().get().validate(); |
| 137 | + } else { |
| 138 | + fileBased().get().validate(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** Configuration for static bearer tokens */ |
| 143 | + @PolarisImmutable |
| 144 | + interface StaticTokenConfig { |
| 145 | + /** Static bearer token value */ |
| 146 | + String value(); |
| 147 | + |
| 148 | + default void validate() { |
| 149 | + checkArgument( |
| 150 | + !Strings.isNullOrEmpty(value()), "Static bearer token value cannot be null or empty"); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** Configuration for file-based bearer tokens */ |
| 155 | + @PolarisImmutable |
| 156 | + interface FileBasedConfig { |
| 157 | + /** Path to file containing bearer token */ |
| 158 | + Path path(); |
| 159 | + |
| 160 | + /** How often to refresh file-based bearer tokens (defaults to 5 minutes if not specified) */ |
| 161 | + Optional<Duration> refreshInterval(); |
| 162 | + |
| 163 | + /** |
| 164 | + * Whether to automatically detect JWT tokens and use their 'exp' field for refresh timing. If |
| 165 | + * true and the token is a valid JWT with an 'exp' claim, the token will be refreshed based on |
| 166 | + * the expiration time minus the buffer, rather than the fixed refresh interval. Defaults to |
| 167 | + * true if not specified. |
| 168 | + */ |
| 169 | + Optional<Boolean> jwtExpirationRefresh(); |
| 170 | + |
| 171 | + /** |
| 172 | + * Buffer time before JWT expiration to refresh the token. Only used when jwtExpirationRefresh |
| 173 | + * is true and the token is a valid JWT. Defaults to 1 minute if not specified. |
| 174 | + */ |
| 175 | + Optional<Duration> jwtExpirationBuffer(); |
| 176 | + |
| 177 | + default void validate() { |
| 178 | + checkArgument( |
| 179 | + refreshInterval().isEmpty() || refreshInterval().get().isPositive(), |
| 180 | + "refreshInterval must be positive"); |
| 181 | + checkArgument( |
| 182 | + jwtExpirationBuffer().isEmpty() || jwtExpirationBuffer().get().isPositive(), |
| 183 | + "jwtExpirationBuffer must be positive"); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments