Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure www-data owns repo structure and permissions are correct #821

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/LexBoxApi/LexBoxKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static void AddLexBoxApi(this IServiceCollection services,
services.AddScoped<TusService>();
services.AddScoped<TurnstileService>();
services.AddScoped<IHgService, HgService>();
services.AddHostedService<HgService>();
myieye marked this conversation as resolved.
Show resolved Hide resolved
services.AddTransient<HgWebHealthCheck>();
services.AddScoped<IIsLanguageForgeProjectDataLoader, IsLanguageForgeProjectDataLoader>();
services.AddScoped<ILexProxyService, LexProxyService>();
Expand Down
27 changes: 25 additions & 2 deletions backend/LexBoxApi/Services/HgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace LexBoxApi.Services;

public partial class HgService : IHgService
public partial class HgService : IHgService, IHostedService
{
private const string DELETED_REPO_FOLDER = "_____deleted_____";
private const string TEMP_REPO_FOLDER = "_____temp_____";
Expand Down Expand Up @@ -292,7 +292,8 @@ private async Task<HttpContent> ExecuteHgCommandServerCommand(string code, strin
return response.Content;
}

private static readonly string[] InvalidRepoNames = { DELETED_REPO_FOLDER, TEMP_REPO_FOLDER, "api" };
private static readonly string[] SpecialDirectoryNames = [DELETED_REPO_FOLDER, TEMP_REPO_FOLDER];
private static readonly IEnumerable<string> InvalidRepoNames = SpecialDirectoryNames.Append("api");
myieye marked this conversation as resolved.
Show resolved Hide resolved

private void AssertIsSafeRepoName(string name)
{
Expand Down Expand Up @@ -366,6 +367,28 @@ public static string DetermineProjectUrlPrefix(HgType type, HgConfig hgConfig)
$"Unknown request, HG request type: {type}")
};
}

public Task StartAsync(CancellationToken cancellationToken)
{
var repoContainerDirectories = SpecialDirectoryNames
.Concat(Enumerable.Range('a', 'z' - 'a' + 1).Select(c => ((char)c).ToString()))
.Concat(Enumerable.Range(0, 10).Select(c => c.ToString()));

foreach (var directory in repoContainerDirectories)
{
var path = Path.Combine(_options.Value.RepoPath, directory);
var dirInfo = Directory.CreateDirectory(path);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
dirInfo.UnixFileMode = Permissions;
}

return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}

public class LogResponse
Expand Down
18 changes: 0 additions & 18 deletions deployment/base/hg-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,3 @@ spec:
items:
- key: hgweb.hgrc
path: hgweb.hgrc

initContainers:
- name: init-repo-structure
securityContext:
runAsUser: 33
runAsGroup: 33 # www-data
runAsNonRoot: true
image: busybox:1.36.1
command:
- 'sh'
- '-c'
- |
cd /repos
mkdir -p a b c d e f g h i j k l m n o p q r s t u v w x y z
mkdir -p 0 1 2 3 4 5 6 7 8 9
volumeMounts:
- name: repos
mountPath: /repos
18 changes: 18 additions & 0 deletions deployment/base/lexbox-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,21 @@ spec:
configMapKeyRef:
name: app-config
key: environment-name
- name: set-repo-structure-owner-to-www-data
securityContext:
# Make sure we're authorized to set ownership
runAsUser: 0
runAsGroup: 0
runAsNonRoot: false
image: busybox:1.36.1
command:
- 'sh'
- '-c'
- |
cd /repos
chown www-data:www-data .
# Only necessary if directories already exist with the wrong ownership
find . -maxdepth 1 -type d ! -name lost+found -exec chown www-data:www-data {} +
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
volumeMounts:
- name: repos
mountPath: /repos
Loading