2323import java .util .regex .Pattern ;
2424
2525public class CertificateManager {
26- // 证书缓存
2726 private static final ConcurrentHashMap <String , ServerCertificateInfo > certificateCache = new ConcurrentHashMap <>();
2827
2928 /**
@@ -87,7 +86,6 @@ public static ServerCertificateInfo getServerCertificateFromCache(String ep) {
8786 }
8887
8988 public static ServerCertificateInfo getServerCertificate (String apiKey , String baseUrl , String ep ) throws IOException {
90- // 首先检查内存缓存,用ep作为key
9189 if (hasCertificateInCache (ep )) {
9290 return getServerCertificateFromCache (ep );
9391 }
@@ -98,18 +96,13 @@ public static ServerCertificateInfo getServerCertificate(String apiKey, String b
9896
9997 String certificate ;
10098
101- // 1. 首先尝试从本地文件加载证书
10299 certificate = loadCertificateLocally (ep );
103100 if (certificate != null ) {
104101 return createCertificateInfo (certificate , ep );
105102 }
106103
107- // 2. 使用API Key方式获取证书
108- else {
109- certificate = loadCertificateByApiKey (baseUrl , apiKey , ep , aiccEnabled );
110- }
104+ certificate = loadCertificateByApiKey (baseUrl , apiKey , ep , aiccEnabled );
111105
112- // 保存证书到本地缓存
113106 saveCertificateLocally (ep , certificate );
114107
115108 return createCertificateInfo (certificate , ep );
@@ -121,7 +114,6 @@ public static ServerCertificateInfo getServerCertificate(String apiKey, String b
121114
122115 public static String [] getCertInfo (String certPem ) {
123116 try {
124- // 使用try-with-resources自动管理PEMParser资源
125117 try (PEMParser pemParser = new PEMParser (new StringReader (certPem ))) {
126118 Object object = pemParser .readObject ();
127119
@@ -141,16 +133,15 @@ public static String[] getCertInfo(String certPem) {
141133
142134 if (ringPattern .matcher (firstDns ).matches () &&
143135 keyPattern .matcher (secondDns ).matches ()) {
144- String ringId = firstDns .substring (5 ); // ring. 5个字符
145- String keyId = secondDns .substring (4 ); // key. 4个字符
136+ String ringId = firstDns .substring (5 );
137+ String keyId = secondDns .substring (4 );
146138 return new String []{ringId , keyId };
147139 }
148140 }
149141 }
150142 }
151143 }
152144 } catch (Exception e ) {
153- // 异常处理
154145 throw new RuntimeException ("Failed to parse certificate to get ring_id and key_id" , e );
155146 }
156147 return new String []{"" , "" };
@@ -167,23 +158,19 @@ public static String loadCertificateLocally(String ep) throws IOException {
167158 File certFile = new File (certFilePath );
168159
169160 if (certFile .exists ()) {
170- // 检查证书是否过期(是否超过14天)
171161 long lastModifiedSeconds = certFile .lastModified () / 1000 ;
172162 long currentTimeSeconds = System .currentTimeMillis () / 1000 ;
173163 long timeDifferenceSeconds = currentTimeSeconds - lastModifiedSeconds ;
174- long certExpirationSeconds = 14L * 24 * 60 * 60 ; // 14天,以秒为单位
164+ long certExpirationSeconds = 14L * 24 * 60 * 60 ;
175165 if (timeDifferenceSeconds <= certExpirationSeconds ) {
176166 String certPem = new String (java .nio .file .Files .readAllBytes (certFile .toPath ()), StandardCharsets .UTF_8 );
177167
178- // 检查证书是否完整(与AICC/PCA兼容性检查)
179168 String [] certInfo = getCertInfo (certPem );
180169 String ringId = certInfo [0 ];
181170 String keyId = certInfo [1 ];
182171
183172 boolean aiccEnabled = "AICC" .equals (System .getenv ("VOLC_ARK_ENCRYPTION" ));
184173
185- // 1. 非AICC模式:即使ring或key为空也可以接受
186- // 2. AICC模式:ring和key都必须不为空
187174 if ((ringId .isEmpty () || keyId .isEmpty ()) && !aiccEnabled ) {
188175 return certPem ;
189176 }
@@ -192,7 +179,6 @@ public static String loadCertificateLocally(String ep) throws IOException {
192179 }
193180 }
194181
195- // 证书过期或不满足条件,删除文件
196182 certFile .delete ();
197183 }
198184 } catch (Exception e ) {
@@ -346,7 +332,6 @@ public static void saveCertificateLocally(String ep, String certificate) throws
346332 String certStoragePath = getCertStoragePath ();
347333 String certFilePath = certStoragePath + File .separator + ep + ".pem" ;
348334
349- // 确保目录存在
350335 File storageDir = new File (certStoragePath );
351336 if (!storageDir .exists ()) {
352337 if (!storageDir .mkdirs ()) {
@@ -355,7 +340,6 @@ public static void saveCertificateLocally(String ep, String certificate) throws
355340 }
356341 }
357342
358- // 写入证书文件
359343 java .nio .file .Files .write (
360344 Paths .get (certFilePath ),
361345 certificate .getBytes (StandardCharsets .UTF_8 )
@@ -387,15 +371,12 @@ public static void cacheServerCertificate(String cacheKey, PublicKey publicKey,
387371 */
388372 public static PublicKey extractPublicKeyFromCertificate (String certificate ) throws GeneralSecurityException {
389373 try {
390- // 移除PEM头尾
391374 String certContent = certificate .replace ("-----BEGIN CERTIFICATE-----" , "" )
392375 .replace ("-----END CERTIFICATE-----" , "" )
393376 .replaceAll ("\\ s" , "" );
394377
395- // 解码Base64
396378 byte [] certBytes = Base64 .getDecoder ().decode (certContent );
397379
398- // 解析证书
399380 CertificateFactory certFactory = CertificateFactory .getInstance ("X.509" );
400381 X509Certificate x509Cert = (X509Certificate ) certFactory .generateCertificate (
401382 new java .io .ByteArrayInputStream (certBytes ));
@@ -420,7 +401,6 @@ public static ServerCertificateInfo createCertificateInfo(String certificate, St
420401 ServerCertificateInfo certInfo =
421402 new ServerCertificateInfo (publicKey , ringId , keyId );
422403
423- // 缓存到内存
424404 cacheServerCertificate (ep , publicKey , ringId , keyId );
425405
426406 return certInfo ;
0 commit comments