|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference; |
| 9 | + |
| 10 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 11 | + |
| 12 | +import org.elasticsearch.client.Request; |
| 13 | +import org.elasticsearch.common.Strings; |
| 14 | +import org.elasticsearch.common.settings.SecureString; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 17 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 18 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 19 | +import org.elasticsearch.test.cluster.util.resource.Resource; |
| 20 | +import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; |
| 21 | +import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.ClassRule; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +public class InferenceWithSecurityRestIT extends ESClientYamlSuiteTestCase { |
| 30 | + |
| 31 | + private static final String TEST_ADMIN_USERNAME = "x_pack_rest_user"; |
| 32 | + private static final String TEST_ADMIN_PASSWORD = "x-pack-test-password"; |
| 33 | + |
| 34 | + private static final String INFERENCE_USERNAME = "inference_user"; |
| 35 | + private static final String INFERENCE_PASSWORD = "inference_user_password"; |
| 36 | + |
| 37 | + @ClassRule |
| 38 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 39 | + .systemProperty("tests.seed", System.getProperty("tests.seed")) |
| 40 | + .setting("xpack.security.enabled", "true") |
| 41 | + .setting("xpack.license.self_generated.type", "trial") |
| 42 | + .rolesFile(Resource.fromClasspath("roles.yml")) |
| 43 | + .user(TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD) // admin user for setup and teardown |
| 44 | + .user(INFERENCE_USERNAME, INFERENCE_PASSWORD, "monitor_only_user", false) |
| 45 | + .plugin("inference-service-test") |
| 46 | + .distribution(DistributionType.DEFAULT) |
| 47 | + .build(); |
| 48 | + |
| 49 | + public InferenceWithSecurityRestIT(final ClientYamlTestCandidate testCandidate) { |
| 50 | + super(testCandidate); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected Settings restClientSettings() { |
| 55 | + String token = basicAuthHeaderValue(INFERENCE_USERNAME, new SecureString(INFERENCE_PASSWORD.toCharArray())); |
| 56 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected Settings restAdminSettings() { |
| 61 | + String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecureString(TEST_ADMIN_PASSWORD.toCharArray())); |
| 62 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).build(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected String getTestRestCluster() { |
| 67 | + return cluster.getHttpAddresses(); |
| 68 | + } |
| 69 | + |
| 70 | + @ParametersFactory |
| 71 | + public static Iterable<Object[]> parameters() throws Exception { |
| 72 | + return ESClientYamlSuiteTestCase.createParameters(); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public void cleanup() throws Exception { |
| 77 | + for (var model : getAllModels()) { |
| 78 | + var inferenceId = model.get("inference_id"); |
| 79 | + try { |
| 80 | + var endpoint = Strings.format("_inference/%s?force", inferenceId); |
| 81 | + adminClient().performRequest(new Request("DELETE", endpoint)); |
| 82 | + } catch (Exception ex) { |
| 83 | + logger.warn(() -> "failed to delete inference endpoint " + inferenceId, ex); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + static List<Map<String, Object>> getAllModels() throws IOException { |
| 90 | + var request = new Request("GET", "_inference/_all"); |
| 91 | + var response = client().performRequest(request); |
| 92 | + return (List<Map<String, Object>>) entityAsMap(response).get("endpoints"); |
| 93 | + } |
| 94 | +} |
0 commit comments