|
| 1 | +package io.stargate.sgv2.jsonapi.service.embedding.operation; |
| 2 | + |
| 3 | +import io.smallrye.mutiny.Uni; |
| 4 | +import io.stargate.sgv2.jsonapi.api.request.EmbeddingCredentials; |
| 5 | +import io.stargate.sgv2.jsonapi.api.request.tenant.Tenant; |
| 6 | +import io.stargate.sgv2.jsonapi.syncservice.SyncServiceClient; |
| 7 | +import java.util.*; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +/** |
| 12 | + * A decorator that wraps a direct {@link EmbeddingProvider} and resolves shared-secret credentials |
| 13 | + * via {@link SyncServiceClient} before each {@link #vectorize} call. |
| 14 | + * |
| 15 | + * <p>When the Embedding Gateway (EGW) is disabled (standalone mode), collections configured with |
| 16 | + * {@code SHARED_SECRET} authentication need their credential names resolved into actual secrets. |
| 17 | + * This provider handles that resolution by calling {@link SyncServiceClient#getCredential} for each |
| 18 | + * entry in the authentication map, then passing the resolved credentials to the delegate provider. |
| 19 | + */ |
| 20 | +public class SyncServiceCredentialResolvingProvider extends EmbeddingProvider { |
| 21 | + |
| 22 | + private static final Logger LOGGER = |
| 23 | + LoggerFactory.getLogger(SyncServiceCredentialResolvingProvider.class); |
| 24 | + |
| 25 | + private static final String PROVIDER_KEY = "providerKey"; |
| 26 | + private static final String ACCESS_ID = "accessId"; |
| 27 | + private static final String SECRET_KEY = "secretKey"; |
| 28 | + private static final String SECRET_ID = "secretId"; |
| 29 | + |
| 30 | + private final EmbeddingProvider delegate; |
| 31 | + private final SyncServiceClient syncServiceClient; |
| 32 | + private final Map<String, String> authentication; |
| 33 | + private final Tenant tenant; |
| 34 | + private final String authToken; |
| 35 | + |
| 36 | + public SyncServiceCredentialResolvingProvider( |
| 37 | + EmbeddingProvider delegate, |
| 38 | + SyncServiceClient syncServiceClient, |
| 39 | + Map<String, String> authentication, |
| 40 | + Tenant tenant, |
| 41 | + String authToken) { |
| 42 | + super( |
| 43 | + delegate.modelProvider(), |
| 44 | + delegate.providerConfig, |
| 45 | + delegate.modelConfig, |
| 46 | + delegate.serviceConfig, |
| 47 | + delegate.dimension, |
| 48 | + delegate.vectorizeServiceParameters); |
| 49 | + |
| 50 | + this.delegate = delegate; |
| 51 | + this.syncServiceClient = syncServiceClient; |
| 52 | + this.authentication = authentication; |
| 53 | + this.tenant = tenant; |
| 54 | + this.authToken = authToken; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected String errorMessageJsonPtr() { |
| 59 | + // Not used directly — this wrapper never makes HTTP calls itself |
| 60 | + return ""; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Uni<BatchedEmbeddingResponse> vectorize( |
| 65 | + int batchId, |
| 66 | + List<String> texts, |
| 67 | + EmbeddingCredentials embeddingCredentials, |
| 68 | + EmbeddingRequestType embeddingRequestType) { |
| 69 | + |
| 70 | + // Match EGW behavior: if caller already provided credentials via headers, use those |
| 71 | + // directly and skip SyncService resolution |
| 72 | + if (hasHeaderCredentials(embeddingCredentials)) { |
| 73 | + return delegate.vectorize(batchId, texts, embeddingCredentials, embeddingRequestType); |
| 74 | + } |
| 75 | + |
| 76 | + return resolveCredentials() |
| 77 | + .flatMap(resolved -> delegate.vectorize(batchId, texts, resolved, embeddingRequestType)); |
| 78 | + } |
| 79 | + |
| 80 | + private static boolean hasHeaderCredentials(EmbeddingCredentials creds) { |
| 81 | + return creds.apiKey().isPresent() |
| 82 | + || creds.accessId().isPresent() |
| 83 | + || creds.secretId().isPresent(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int maxBatchSize() { |
| 88 | + return delegate.maxBatchSize(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Resolves credentials by calling SyncService for each entry in the authentication map. Each |
| 93 | + * entry's key is the accepted token name (e.g. "providerKey"), and the value is the credential |
| 94 | + * reference name stored in SyncService (e.g. "my-openai-cred"). |
| 95 | + * |
| 96 | + * <p>The SyncService response map is keyed by the credential reference name, so we extract the |
| 97 | + * resolved secret using the credential name as key (matching EGW's EmbeddingServiceImpl |
| 98 | + * behavior). |
| 99 | + */ |
| 100 | + private Uni<EmbeddingCredentials> resolveCredentials() { |
| 101 | + String providerName = modelProvider().apiName(); |
| 102 | + |
| 103 | + // Build parallel SyncService calls and track which accepted token name each one is for |
| 104 | + List<String> acceptedNames = new ArrayList<>(); |
| 105 | + List<String> credNames = new ArrayList<>(); |
| 106 | + List<Uni<Map<String, String>>> resolveUnis = new ArrayList<>(); |
| 107 | + |
| 108 | + for (Map.Entry<String, String> entry : authentication.entrySet()) { |
| 109 | + acceptedNames.add(entry.getKey()); |
| 110 | + credNames.add(entry.getValue()); |
| 111 | + resolveUnis.add( |
| 112 | + syncServiceClient.getCredential(authToken, tenant, providerName, entry.getValue())); |
| 113 | + } |
| 114 | + |
| 115 | + return Uni.join() |
| 116 | + .all(resolveUnis) |
| 117 | + .andFailFast() |
| 118 | + .map( |
| 119 | + results -> { |
| 120 | + // For each resolved result, extract the secret using the credential name as key |
| 121 | + // (SyncService response is keyed by credential reference name, not accepted name) |
| 122 | + Map<String, String> resolvedByAcceptedName = new HashMap<>(); |
| 123 | + for (int i = 0; i < results.size(); i++) { |
| 124 | + Map<String, String> credMap = results.get(i); |
| 125 | + if (credMap != null) { |
| 126 | + String credName = credNames.get(i); |
| 127 | + String acceptedName = acceptedNames.get(i); |
| 128 | + String resolvedValue = credMap.get(credName); |
| 129 | + if (resolvedValue != null) { |
| 130 | + resolvedByAcceptedName.put(acceptedName, resolvedValue); |
| 131 | + } else { |
| 132 | + LOGGER.warn( |
| 133 | + "SyncService response for credential '{}' (provider '{}') did not contain" |
| 134 | + + " expected key '{}'; available keys: {}", |
| 135 | + credName, |
| 136 | + providerName, |
| 137 | + credName, |
| 138 | + credMap.keySet()); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return buildEmbeddingCredentials(resolvedByAcceptedName); |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Maps resolved credential values (keyed by accepted token name) to {@link EmbeddingCredentials}. |
| 148 | + * The accepted token names come from the provider's SHARED_SECRET config (e.g. "providerKey", |
| 149 | + * "accessId", "secretKey"). |
| 150 | + */ |
| 151 | + private EmbeddingCredentials buildEmbeddingCredentials( |
| 152 | + Map<String, String> resolvedByAcceptedName) { |
| 153 | + var apiKey = Optional.ofNullable(resolvedByAcceptedName.get(PROVIDER_KEY)); |
| 154 | + var accessId = Optional.ofNullable(resolvedByAcceptedName.get(ACCESS_ID)); |
| 155 | + var secretId = |
| 156 | + Optional.ofNullable( |
| 157 | + resolvedByAcceptedName.containsKey(SECRET_KEY) |
| 158 | + ? resolvedByAcceptedName.get(SECRET_KEY) |
| 159 | + : resolvedByAcceptedName.get(SECRET_ID)); |
| 160 | + return new EmbeddingCredentials(tenant, apiKey, accessId, secretId, Optional.of(authToken)); |
| 161 | + } |
| 162 | +} |
0 commit comments