|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.security; |
| 21 | + |
| 22 | +import org.elasticsearch.client.security.support.CertificateInfo; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.test.ESTestCase; |
| 25 | +import org.elasticsearch.test.EqualsHashCodeTestUtils; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester; |
| 34 | + |
| 35 | +public class GetSslCertificatesResponseTests extends ESTestCase { |
| 36 | + public void testFromXContent() throws IOException { |
| 37 | + xContentTester( |
| 38 | + this::createParser, |
| 39 | + this::createTestInstance, |
| 40 | + this::toXContent, |
| 41 | + GetSslCertificatesResponse::fromXContent) |
| 42 | + .supportsUnknownFields(false) |
| 43 | + .test(); |
| 44 | + } |
| 45 | + public void testEqualsAndHashCode() { |
| 46 | + final GetSslCertificatesResponse reponse = createTestInstance(); |
| 47 | + EqualsHashCodeTestUtils.checkEqualsAndHashCode(reponse, this::copy, |
| 48 | + this::mutate); |
| 49 | + } |
| 50 | + |
| 51 | + protected GetSslCertificatesResponse createTestInstance() { |
| 52 | + final CertificateInfo info1 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", |
| 53 | + "CN=Elastic Certificate Tool Autogenerated CA", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", |
| 54 | + false, "2021-01-15T20:42:49.000Z"); |
| 55 | + final CertificateInfo info2 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "ca", |
| 56 | + "CN=Elastic Certificate Tool Autogenerated CA", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", |
| 57 | + false, "2021-01-15T20:42:49.000Z"); |
| 58 | + final CertificateInfo info3 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", |
| 59 | + "CN=instance", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", |
| 60 | + true, "2021-01-15T20:44:32.000Z"); |
| 61 | + return new GetSslCertificatesResponse(Arrays.asList(info1, info2, info3)); |
| 62 | + } |
| 63 | + |
| 64 | + private void toXContent(GetSslCertificatesResponse response, XContentBuilder builder) throws IOException { |
| 65 | + builder.startArray(); |
| 66 | + for (CertificateInfo info : response.getCertificates()){ |
| 67 | + builder.startObject(); |
| 68 | + builder.field(CertificateInfo.PATH.getPreferredName(), info.getPath()); |
| 69 | + builder.field(CertificateInfo.FORMAT.getPreferredName(), info.getFormat()); |
| 70 | + builder.field(CertificateInfo.ALIAS.getPreferredName(), info.getAlias()); |
| 71 | + builder.field(CertificateInfo.SUBJECT_DN.getPreferredName(), info.getSubjectDn()); |
| 72 | + builder.field(CertificateInfo.SERIAL_NUMBER.getPreferredName(), info.getSerialNumber()); |
| 73 | + builder.field(CertificateInfo.HAS_PRIVATE_KEY.getPreferredName(), info.hasPrivateKey()); |
| 74 | + builder.field(CertificateInfo.EXPIRY.getPreferredName(), info.getExpiry()); |
| 75 | + builder.endObject(); |
| 76 | + } |
| 77 | + builder.endArray(); |
| 78 | + } |
| 79 | + |
| 80 | + private GetSslCertificatesResponse copy(GetSslCertificatesResponse original) { |
| 81 | + final List<CertificateInfo> infoList = new ArrayList<>(original.getCertificates()); |
| 82 | + return new GetSslCertificatesResponse(infoList); |
| 83 | + } |
| 84 | + |
| 85 | + private GetSslCertificatesResponse mutate(GetSslCertificatesResponse original) { |
| 86 | + final int i = randomIntBetween(1,5); |
| 87 | + final List<CertificateInfo> infoList = new ArrayList<>(original.getCertificates()); |
| 88 | + switch (i) { |
| 89 | + case 1: |
| 90 | + infoList.remove(0); |
| 91 | + return new GetSslCertificatesResponse(infoList); |
| 92 | + case 2: |
| 93 | + final CertificateInfo info = new CertificateInfo("certs/elastic-certificates.crt", "PEM", "instance", |
| 94 | + "CN=instance2", "a20f0ee901e8f64t33ff633e5cd5437cdb4137", |
| 95 | + true, "2028-01-15T20:44:32.000Z"); |
| 96 | + infoList.add(info); |
| 97 | + return new GetSslCertificatesResponse(infoList); |
| 98 | + case 3: |
| 99 | + final CertificateInfo info2 = new CertificateInfo("certs/elastic-certificates.p12", "PKCS12", "instance", |
| 100 | + "CN=instance1", "a20f0ee901e8f69dc633ff633e5cd5437cdb4137", |
| 101 | + true, "2021-01-15T20:44:32.000Z"); |
| 102 | + infoList.remove(2); |
| 103 | + infoList.add(info2); |
| 104 | + return new GetSslCertificatesResponse(infoList); |
| 105 | + default: |
| 106 | + return new GetSslCertificatesResponse(Collections.emptyList()); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments