-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEnsureSecurityUsersInDefaultRealmAreAdded.cs
62 lines (52 loc) · 2.85 KB
/
EnsureSecurityUsersInDefaultRealmAreAdded.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.IO;
using Elastic.Elasticsearch.Managed.ConsoleWriters;
namespace Elastic.Elasticsearch.Ephemeral.Tasks.BeforeStartNodeTasks.XPack
{
public class EnsureSecurityUsersInDefaultRealmAreAdded : ClusterComposeTask
{
public override void Run(IEphemeralCluster<EphemeralClusterConfiguration> cluster)
{
if (!cluster.ClusterConfiguration.EnableSecurity) return;
var config = cluster.ClusterConfiguration;
var fileSystem = cluster.FileSystem;
var v = config.Version;
var xpackConfigFolder =
v >= "6.3.0" ? fileSystem.ConfigPath : Path.Combine(fileSystem.ConfigPath, "x-pack");
;
var xpackConfigFolderCached = v >= "6.3.0"
? Path.Combine(fileSystem.LocalFolder, cluster.GetCacheFolderName(), "config")
: Path.Combine(fileSystem.LocalFolder, cluster.GetCacheFolderName(), "config", "x-pack");
var usersFile = Path.Combine(xpackConfigFolder, "users");
var usersFileCached = usersFile.Replace(xpackConfigFolder, xpackConfigFolderCached);
var usersRolesFile = Path.Combine(xpackConfigFolder, "users_roles");
var usersRolesFileCached = usersRolesFile.Replace(xpackConfigFolder, xpackConfigFolderCached);
var userCachedFileInfo = new FileInfo(usersFileCached);
if (userCachedFileInfo.Exists && userCachedFileInfo.Length > 0 &&
cluster.ClusterConfiguration.CacheEsHomeInstallation)
{
cluster.Writer?.WriteDiagnostic(
$"{{{nameof(EnsureSecurityUsersInDefaultRealmAreAdded)}}} using cached users and users_roles files from {{{xpackConfigFolderCached}}}");
if (!Directory.Exists(xpackConfigFolder)) Directory.CreateDirectory(xpackConfigFolder);
if (!File.Exists(usersFile)) File.Copy(usersFileCached, usersFile);
if (!File.Exists(usersRolesFile)) File.Copy(usersRolesFileCached, usersRolesFile);
}
else
{
var folder = v.Major >= 5 ? v >= "6.3.0" ? string.Empty : "x-pack" : "shield";
var binary = v.Major >= 5 ? v >= "6.3.0" ? "elasticsearch-users" : "users" : "esusers";
var h = fileSystem.ElasticsearchHome;
var pluginFolder = v >= "6.3.0" ? Path.Combine(h, "bin") : Path.Combine(h, "bin", folder);
var pluginBat = Path.Combine(pluginFolder, binary) + BinarySuffix;
foreach (var cred in ClusterAuthentication.AllUsers)
ExecuteBinary(cluster.ClusterConfiguration, cluster.Writer, pluginBat,
$"adding user {cred.Username}", $"useradd {cred.Username} -p {cred.Password} -r {cred.Role}");
if (!Directory.Exists(xpackConfigFolderCached)) Directory.CreateDirectory(xpackConfigFolderCached);
if (!File.Exists(usersFileCached)) File.Copy(usersFile, usersFileCached);
if (!File.Exists(usersRolesFileCached)) File.Copy(usersRolesFile, usersRolesFileCached);
}
}
}
}