|
| 1 | +/* |
| 2 | + * Copyright 2025 IEXEC BLOCKCHAIN TECH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.iexec.sms.tee.session.tdx; |
| 18 | + |
| 19 | +import com.iexec.commons.poco.tee.TeeFramework; |
| 20 | +import com.iexec.sms.api.config.TeeAppProperties; |
| 21 | +import com.iexec.sms.tee.ConditionalOnTeeFramework; |
| 22 | +import com.iexec.sms.tee.session.base.SecretEnclaveBase; |
| 23 | +import com.iexec.sms.tee.session.base.SecretSessionBase; |
| 24 | +import com.iexec.sms.tee.session.base.SecretSessionBaseService; |
| 25 | +import com.iexec.sms.tee.session.generic.TeeSessionGenerationException; |
| 26 | +import com.iexec.sms.tee.session.generic.TeeSessionRequest; |
| 27 | +import com.iexec.sms.tee.session.tdx.storage.TdxSession; |
| 28 | +import org.springframework.stereotype.Service; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +@Service |
| 34 | +@ConditionalOnTeeFramework(frameworks = TeeFramework.TDX) |
| 35 | +public class TdxSessionMakerService { |
| 36 | + public static final String TDX_SESSION_VERSION = "0.1.0"; |
| 37 | + private final SecretSessionBaseService secretSessionBaseService; |
| 38 | + |
| 39 | + public TdxSessionMakerService(final SecretSessionBaseService secretSessionBaseService) { |
| 40 | + this.secretSessionBaseService = secretSessionBaseService; |
| 41 | + } |
| 42 | + |
| 43 | + public TdxSession generateSession(final TeeSessionRequest request) throws TeeSessionGenerationException { |
| 44 | + final SecretSessionBase baseSession = secretSessionBaseService.getSecretsTokens(request); |
| 45 | + final List<TdxSession.Service> tdxEnclaves = new ArrayList<>(); |
| 46 | + if (baseSession.getPreCompute() != null) { |
| 47 | + tdxEnclaves.add(toTdxEnclave(baseSession.getPreCompute(), request.getTeeServicesProperties().getPreComputeProperties())); |
| 48 | + } |
| 49 | + // FIXME fingerprint should be dapp checksum from TaskDescription after commons-poco update |
| 50 | + tdxEnclaves.add(toTdxEnclave(baseSession.getAppCompute(), request.getTaskDescription().getAppUri(), "")); |
| 51 | + tdxEnclaves.add(toTdxEnclave(baseSession.getPostCompute(), request.getTeeServicesProperties().getPostComputeProperties())); |
| 52 | + return new TdxSession(request.getSessionId(), TDX_SESSION_VERSION, List.copyOf(tdxEnclaves)); |
| 53 | + } |
| 54 | + |
| 55 | + private TdxSession.Service toTdxEnclave(final SecretEnclaveBase enclaveBase, final TeeAppProperties teeAppProperties) { |
| 56 | + return toTdxEnclave(enclaveBase, teeAppProperties.getImage(), teeAppProperties.getFingerprint()); |
| 57 | + } |
| 58 | + |
| 59 | + private TdxSession.Service toTdxEnclave(final SecretEnclaveBase enclaveBase, final String image_name, final String fingerprint) { |
| 60 | + return new TdxSession.Service( |
| 61 | + enclaveBase.getName(), image_name, fingerprint, enclaveBase.getEnvironment() |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
0 commit comments