1212package org .opensearch .security .dlic .rest .api .ssl ;
1313
1414import java .io .IOException ;
15+ import java .util .HashMap ;
1516import java .util .List ;
17+ import java .util .Locale ;
1618import java .util .Map ;
1719import java .util .Set ;
1820
2224import org .opensearch .core .common .io .stream .Writeable ;
2325import org .opensearch .core .xcontent .ToXContent ;
2426import org .opensearch .core .xcontent .XContentBuilder ;
25- import org .opensearch .security .ssl .config .CertType ;
2627
2728public class CertificatesInfo implements Writeable , ToXContent {
2829 private final Map <String , List <CertificateInfo >> certificates ;
@@ -32,45 +33,32 @@ public CertificatesInfo(final Map<String, List<CertificateInfo>> certificates) {
3233 }
3334
3435 public CertificatesInfo (final StreamInput in ) throws IOException {
35- if (in .getVersion ().onOrAfter (Version .V_3_2_0 )) {
36- certificates = in .readMap (StreamInput ::readString , listIn -> listIn .readList (CertificateInfo ::new ));
36+ if (in .getVersion ().before (Version .V_3_0_0 )) {
37+ Map <CertificateType_2_19 , List <CertificateInfo >> compatMap = in .readMap (
38+ keyIn -> keyIn .readEnum (CertificateType_2_19 .class ),
39+ listIn -> listIn .readList (CertificateInfo ::new )
40+ );
41+ certificates = new HashMap <>();
42+ for (Map .Entry <CertificateType_2_19 , List <CertificateInfo >> entry : compatMap .entrySet ()) {
43+ certificates .put (entry .getKey ().value (), entry .getValue ());
44+ }
3745 } else {
38- /*
39- Previous versions represent cert types with an enum and serialize based on
40- enum ordinal. To maintain backwards compatibility we fall back to mapping these
41- enum ordinals to the appropriate native certificate type.
42- */
43- certificates = in .readMap ((StreamInput streamIn ) -> switch (streamIn .readEnum (CertType .LegacyCertType .class )) {
44- case CertType .LegacyCertType .HTTP -> CertType .HTTP .id ();
45- case CertType .LegacyCertType .TRANSPORT -> CertType .TRANSPORT .id ();
46- case CertType .LegacyCertType .TRANSPORT_CLIENT -> CertType .TRANSPORT_CLIENT .id ();
47- }, listIn -> listIn .readList (CertificateInfo ::new ));
46+ certificates = in .readMap (StreamInput ::readString , listIn -> listIn .readList (CertificateInfo ::new ));
4847 }
4948 }
5049
5150 @ Override
5251 public void writeTo (StreamOutput out ) throws IOException {
53- if (out .getVersion ().onOrAfter (Version .V_3_2_0 )) {
54- out .writeMap (certificates , StreamOutput ::writeString , StreamOutput ::writeList );
55- } else {
56- /*
57- We need to write only map elements which previous versions will understand.
58- CertTypes are strictly bound to LegacyCertType enum in these versions and only has knowledge of
59- HTTP, TRANSPORT, TRANSPORT_CLIENT.
60- */
61- Set <String > legacyCerts = certificates .keySet ();
62- legacyCerts .retainAll (List .of (CertType .HTTP .id (), CertType .TRANSPORT .id (), CertType .TRANSPORT_CLIENT .id ()));
63- out .writeVInt (legacyCerts .size ());
64- for (String certId : legacyCerts ) {
65- if (CertType .HTTP .id ().equals (certId )) {
66- out .writeEnum (CertType .LegacyCertType .HTTP );
67- } else if (CertType .TRANSPORT .id ().equals (certId )) {
68- out .writeEnum (CertType .LegacyCertType .TRANSPORT );
69- } else if (CertType .TRANSPORT_CLIENT .id ().equals (certId )) {
70- out .writeEnum (CertType .LegacyCertType .TRANSPORT_CLIENT );
52+ if (out .getVersion ().before (Version .V_3_0_0 )) {
53+ Map <CertificateType_2_19 , List <CertificateInfo >> compatMap = new HashMap <>();
54+ for (Map .Entry <String , List <CertificateInfo >> entry : certificates .entrySet ()) {
55+ if (Set .of ("http" , "transport" ).contains (entry .getKey ().toLowerCase (Locale .ROOT ))) {
56+ compatMap .put (CertificateType_2_19 .from (entry .getKey ()), entry .getValue ());
7157 }
72- out .writeList (certificates .get (certId ));
7358 }
59+ out .writeMap (compatMap , StreamOutput ::writeEnum , StreamOutput ::writeList );
60+ } else {
61+ out .writeMap (certificates , StreamOutput ::writeString , StreamOutput ::writeList );
7462 }
7563 }
7664
@@ -82,4 +70,38 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
8270 }
8371 return builder .endObject ();
8472 }
73+
74+ public enum CertificateType_2_19 {
75+ HTTP ("http" ),
76+ TRANSPORT ("transport" ),
77+ ALL ("all" );
78+
79+ private final String value ;
80+
81+ private CertificateType_2_19 (String value ) {
82+ this .value = value ;
83+ }
84+
85+ public static boolean isHttp (final CertificateType_2_19 certificateType ) {
86+ return certificateType == HTTP || certificateType == ALL ;
87+ }
88+
89+ public static boolean isTransport (final CertificateType_2_19 certificateType ) {
90+ return certificateType == TRANSPORT || certificateType == ALL ;
91+ }
92+
93+ public String value () {
94+ return value .toLowerCase (Locale .ROOT );
95+ }
96+
97+ public static CertificateType_2_19 from (final String certType ) {
98+ if (certType == null ) {
99+ return ALL ;
100+ }
101+ for (final var t : values ())
102+ if (t .value .equalsIgnoreCase (certType )) return t ;
103+ throw new IllegalArgumentException ("Invalid certificate type: " + certType );
104+ }
105+
106+ }
85107}
0 commit comments