Skip to content

Commit

Permalink
fix(k8s): use JsonSerializer for auth string
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Aug 9, 2023
1 parent a84f5be commit 49b912f
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/GZCTF/Services/K8sService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Text;
using System.Text.Json;
using GZCTF.Models.Internal;
using GZCTF.Services.Interface;
using GZCTF.Utils;
Expand Down Expand Up @@ -289,23 +290,22 @@ private void InitK8s(bool withAuth, RegistryConfig? registry)
}, options.Namespace);
}

if (withAuth && registry is not null)
if (withAuth && registry is not null && registry.ServerAddress is not null)
{
// check if the UserName and Password
// will inject the json and make it invalid
foreach (var chr in $"{registry.UserName}{registry.Password}")
var auth = Codec.Base64.Encode($"{registry.UserName}:{registry.Password}");
var dockerjsonObj = new
{
if (":@\"\\".Contains(chr))
{
logger.SystemLog("Registry 用户名或密码中包含非法字符", TaskStatus.Failed, LogLevel.Error);
throw new ArgumentException("Registry 用户名或密码中包含非法字符");
auths = new Dictionary<string, object> {
{
registry.ServerAddress, new {
auth,
username = registry.UserName,
password = registry.Password
}
}
}
}

var auth = Codec.Base64.Encode($"{registry.UserName}:{registry.Password}");
var dockerjson = $"{{\"auths\":{{\"{registry.ServerAddress}\":{{\"auth\":\"{auth}\"," +
$"\"username\":\"{registry.UserName}\",\"password\":\"{registry.Password}\"}}}}}}";
var dockerjsonBytes = Encoding.ASCII.GetBytes(dockerjson);
};
var dockerjsonBytes = JsonSerializer.SerializeToUtf8Bytes(dockerjsonObj);
var secret = new V1Secret()
{
Metadata = new V1ObjectMeta()
Expand Down

0 comments on commit 49b912f

Please sign in to comment.