|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package org.eclipse.hawkbit.mcp.server.client; |
| 11 | + |
| 12 | +import com.github.benmanes.caffeine.cache.Cache; |
| 13 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 14 | +import feign.FeignException; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.eclipse.hawkbit.mcp.server.config.HawkBitMcpProperties; |
| 17 | +import org.eclipse.hawkbit.mgmt.rest.api.MgmtTenantManagementRestApi; |
| 18 | +import org.eclipse.hawkbit.sdk.HawkbitClient; |
| 19 | +import org.eclipse.hawkbit.sdk.Tenant; |
| 20 | +import org.springframework.http.ResponseEntity; |
| 21 | +import org.springframework.stereotype.Component; |
| 22 | + |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.security.MessageDigest; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.util.HexFormat; |
| 27 | + |
| 28 | +/** |
| 29 | + * Validates authentication credentials against hawkBit REST API using the SDK. |
| 30 | + */ |
| 31 | +@Slf4j |
| 32 | +@Component |
| 33 | +public class HawkBitAuthenticationValidator { |
| 34 | + |
| 35 | + private final HawkbitClient hawkbitClient; |
| 36 | + private final Tenant dummyTenant; |
| 37 | + private final Cache<String, Boolean> validationCache; |
| 38 | + private final boolean enabled; |
| 39 | + |
| 40 | + public HawkBitAuthenticationValidator(HawkbitClient hawkbitClient, |
| 41 | + Tenant dummyTenant, |
| 42 | + HawkBitMcpProperties properties) { |
| 43 | + this.hawkbitClient = hawkbitClient; |
| 44 | + this.dummyTenant = dummyTenant; |
| 45 | + this.enabled = properties.getValidation().isEnabled(); |
| 46 | + |
| 47 | + this.validationCache = Caffeine.newBuilder() |
| 48 | + .expireAfterWrite(properties.getValidation().getCacheTtl()) |
| 49 | + .maximumSize(properties.getValidation().getCacheMaxSize()) |
| 50 | + .build(); |
| 51 | + |
| 52 | + log.info("Authentication validation {} with cache TTL={}, maxSize={}", |
| 53 | + enabled ? "enabled" : "disabled", |
| 54 | + properties.getValidation().getCacheTtl(), |
| 55 | + properties.getValidation().getCacheMaxSize()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Validates the given authorization header against hawkBit. |
| 60 | + * @param authHeader the Authorization header value |
| 61 | + * @return validation result |
| 62 | + */ |
| 63 | + public ValidationResult validate(String authHeader) { |
| 64 | + if (!enabled) { |
| 65 | + return ValidationResult.VALID; |
| 66 | + } |
| 67 | + |
| 68 | + if (authHeader == null || authHeader.isBlank()) { |
| 69 | + return ValidationResult.MISSING_CREDENTIALS; |
| 70 | + } |
| 71 | + |
| 72 | + String cacheKey = hashAuthHeader(authHeader); |
| 73 | + Boolean cachedResult = validationCache.getIfPresent(cacheKey); |
| 74 | + |
| 75 | + if (cachedResult != null) { |
| 76 | + log.debug("Authentication validation cache hit: valid={}", cachedResult); |
| 77 | + return cachedResult ? ValidationResult.VALID : ValidationResult.INVALID_CREDENTIALS; |
| 78 | + } |
| 79 | + |
| 80 | + return validateWithHawkBit(cacheKey); |
| 81 | + } |
| 82 | + |
| 83 | + private ValidationResult validateWithHawkBit(String cacheKey) { |
| 84 | + log.debug("Validating authentication against hawkBit using SDK"); |
| 85 | + |
| 86 | + try { |
| 87 | + MgmtTenantManagementRestApi tenantApi = hawkbitClient.mgmtService( |
| 88 | + MgmtTenantManagementRestApi.class, dummyTenant); |
| 89 | + |
| 90 | + ResponseEntity<?> response = tenantApi.getTenantConfiguration(); |
| 91 | + int statusCode = response.getStatusCode().value(); |
| 92 | + |
| 93 | + if (statusCode >= 200 && statusCode < 300) { |
| 94 | + log.debug("Authentication valid (status={})", statusCode); |
| 95 | + validationCache.put(cacheKey, true); |
| 96 | + return ValidationResult.VALID; |
| 97 | + } else { |
| 98 | + log.warn("Unexpected status from hawkBit during auth validation: {}", statusCode); |
| 99 | + return ValidationResult.HAWKBIT_ERROR; |
| 100 | + } |
| 101 | + } catch (FeignException.Unauthorized e) { |
| 102 | + log.debug("Authentication invalid (status=401)"); |
| 103 | + validationCache.put(cacheKey, false); |
| 104 | + return ValidationResult.INVALID_CREDENTIALS; |
| 105 | + } catch (FeignException.Forbidden e) { |
| 106 | + // 403 = Valid credentials but lacks READ_TENANT_CONFIGURATION permission |
| 107 | + // User is authenticated in hawkBit but doesn't have this specific permission |
| 108 | + log.debug("Authentication valid but lacks permission (status=403)"); |
| 109 | + validationCache.put(cacheKey, true); |
| 110 | + return ValidationResult.VALID; |
| 111 | + } catch (FeignException e) { |
| 112 | + log.warn("Error validating authentication against hawkBit: {} - {}", |
| 113 | + e.getClass().getSimpleName(), e.getMessage()); |
| 114 | + return ValidationResult.HAWKBIT_ERROR; |
| 115 | + } catch (Exception e) { |
| 116 | + // Unexpected errors, don't cache, fail closed |
| 117 | + log.warn("Unexpected error validating authentication against hawkBit: {}", e.getMessage()); |
| 118 | + return ValidationResult.HAWKBIT_ERROR; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private String hashAuthHeader(String authHeader) { |
| 123 | + try { |
| 124 | + MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 125 | + byte[] hash = digest.digest(authHeader.getBytes(StandardCharsets.UTF_8)); |
| 126 | + return HexFormat.of().formatHex(hash); |
| 127 | + } catch (NoSuchAlgorithmException e) { |
| 128 | + // SHA-256 is always available |
| 129 | + throw new McpAuthenticationException("SHA-256 not available." + e.getMessage()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Result of authentication validation. |
| 135 | + */ |
| 136 | + public enum ValidationResult { |
| 137 | + /** |
| 138 | + * Credentials are valid (authenticated user). |
| 139 | + */ |
| 140 | + VALID, |
| 141 | + |
| 142 | + /** |
| 143 | + * No credentials provided. |
| 144 | + */ |
| 145 | + MISSING_CREDENTIALS, |
| 146 | + |
| 147 | + /** |
| 148 | + * Credentials are invalid (401 from hawkBit). |
| 149 | + */ |
| 150 | + INVALID_CREDENTIALS, |
| 151 | + |
| 152 | + /** |
| 153 | + * hawkBit is unavailable or returned unexpected error. |
| 154 | + */ |
| 155 | + HAWKBIT_ERROR |
| 156 | + } |
| 157 | +} |
0 commit comments