apply plugin: 'elasticsearch.legacy-java-rest-test'

dependencies {
  javaRestTestImplementation(testArtifact(project(xpackModule('core'))))
  javaRestTestImplementation project(path: xpackModule('transform'))
  javaRestTestImplementation project(path: xpackModule('transform:qa:common'))
}

// location for keys and certificates
File keystoreDir = new File(project.buildDir, 'keystore')
File nodeKey = file("$keystoreDir/testnode.pem")
File nodeCert = file("$keystoreDir/testnode.crt")
// Add key and certs to test classpath: it expects it there
def copyKeyCerts = tasks.register("copyKeyCerts", Copy) {
  from(project(':x-pack:plugin:core').file('src/test/resources/org/elasticsearch/xpack/security/transport/ssl/certs/simple/')) {
    include 'testnode.crt', 'testnode.pem'
  }
  into keystoreDir
}
// Add keys and cets to javaRestTest classpath: it expects it there
sourceSets.javaRestTest.resources.srcDir(keystoreDir)

tasks.named("processJavaRestTestResources").configure { dependsOn("copyKeyCerts") }
tasks.named("javaRestTest").configure { dependsOn "copyKeyCerts" }

testClusters.matching { it.name == 'javaRestTest' }.configureEach {
  testDistribution = 'DEFAULT'
  setting 'xpack.security.enabled', 'true'
  setting 'xpack.license.self_generated.type', 'trial'
  setting 'xpack.security.authc.token.enabled', 'true'
  setting 'xpack.security.transport.ssl.enabled', 'true'
  setting 'xpack.security.transport.ssl.key', nodeKey.name
  setting 'xpack.security.transport.ssl.certificate', nodeCert.name
  setting 'xpack.security.transport.ssl.verification_mode', 'certificate'
  setting 'xpack.security.audit.enabled', 'false'
  setting 'xpack.license.self_generated.type', 'trial'
  keystore 'bootstrap.password', 'x-pack-test-password'
  keystore 'xpack.security.transport.ssl.secure_key_passphrase', 'testnode'
  numberOfNodes = 3
  // Node roles are configured this way in order to verify redirecting the transform request from the node lacking
  // remote_cluster_client role to the node that is remote_cluster_client.
  nodes."javaRestTest-0".setting 'node.roles', '[data,ingest,master]'
  nodes."javaRestTest-1".setting 'node.roles', '[data,ingest,master,transform]'
  nodes."javaRestTest-2".setting 'node.roles', '[data,ingest,master,transform,remote_cluster_client]'

  extraConfigFile nodeKey.name, nodeKey
  extraConfigFile nodeCert.name, nodeCert
  rolesFile file('roles.yml')
  user username: "x_pack_rest_user", password: "x-pack-test-password"
  user username: "john_junior", password: "x-pack-test-password", role: "transform_admin"
  user username: "bill_senior", password: "x-pack-test-password", role: "transform_admin,source_index_access,dest_index_access"
  user username: "source_and_dest_index_access_only", password: "x-pack-test-password", role: "source_index_access,dest_index_access"
  user username: "transform_user_but_not_admin", password: "x-pack-test-password", role: "transform_user,source_index_access,dest_index_access"
  user username: "fleet_access", password: "x-pack-test-password", role: "transform_admin,fleet_index_access,dest_index_access"
}
