Save original, pre-parsed string from workflow input for the user's custom volume mounts (#1889)

* Save pre parsed string from workflow input for volume mounts that have one

* Use property

* Use named params
This commit is contained in:
Ferenc Hammerl
2022-05-23 12:07:38 +02:00
committed by GitHub
parent 5cca207314
commit 88ee16fb02
2 changed files with 25 additions and 17 deletions

View File

@@ -1,16 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common; using GitHub.Runner.Common;
using GitHub.Runner.Sdk; using GitHub.Runner.Sdk;
using Pipelines = GitHub.DistributedTask.Pipelines; using Pipelines = GitHub.DistributedTask.Pipelines;
using System.Collections.ObjectModel;
using System.Linq;
namespace GitHub.Runner.Worker.Container namespace GitHub.Runner.Worker.Container
{ {
public class ContainerInfo public class ContainerInfo
{ {
private IDictionary<string, string> _userMountVolumes;
private List<MountVolume> _mountVolumes; private List<MountVolume> _mountVolumes;
private IDictionary<string, string> _userPortMappings; private IDictionary<string, string> _userPortMappings;
private List<PortMapping> _portMappings; private List<PortMapping> _portMappings;
@@ -68,8 +68,7 @@ namespace GitHub.Runner.Worker.Container
{ {
foreach (var volume in container.Volumes) foreach (var volume in container.Volumes)
{ {
UserMountVolumes[volume] = volume; MountVolumes.Add(new MountVolume(volume, isUserProvided: true));
MountVolumes.Add(new MountVolume(volume));
} }
} }
@@ -104,19 +103,20 @@ namespace GitHub.Runner.Worker.Container
return _environmentVariables; return _environmentVariables;
} }
} }
public ReadOnlyCollection<MountVolume> UserMountVolumes
public IDictionary<string, string> UserMountVolumes
{ {
get get
{ {
if (_userMountVolumes == null) return MountVolumes.Where(v => !string.IsNullOrEmpty(v.UserProvidedValue)).ToList().AsReadOnly();
{ }
_userMountVolumes = new Dictionary<string, string>(); }
} public ReadOnlyCollection<MountVolume> SystemMountVolumes
return _userMountVolumes; {
get
{
return MountVolumes.Where(v => string.IsNullOrEmpty(v.UserProvidedValue)).ToList().AsReadOnly();
} }
} }
public List<MountVolume> MountVolumes public List<MountVolume> MountVolumes
{ {
get get
@@ -260,18 +260,27 @@ namespace GitHub.Runner.Worker.Container
public class MountVolume public class MountVolume
{ {
public string UserProvidedValue { get; set; }
public MountVolume(string sourceVolumePath, string targetVolumePath, bool readOnly = false) public MountVolume(string sourceVolumePath, string targetVolumePath, bool readOnly = false)
{ {
this.SourceVolumePath = sourceVolumePath; this.SourceVolumePath = sourceVolumePath;
this.TargetVolumePath = targetVolumePath; this.TargetVolumePath = targetVolumePath;
this.ReadOnly = readOnly; this.ReadOnly = readOnly;
} }
public MountVolume(string fromString) public MountVolume(string fromString)
{ {
ParseVolumeString(fromString); ParseVolumeString(fromString);
} }
public MountVolume(string fromString, bool isUserProvided)
{
ParseVolumeString(fromString);
if (isUserProvided)
{
UserProvidedValue = fromString;
}
}
private void ParseVolumeString(string volume) private void ParseVolumeString(string volume)
{ {
var volumeSplit = volume.Split(":"); var volumeSplit = volume.Split(":");

View File

@@ -192,13 +192,12 @@ namespace GitHub.Runner.Worker
{ {
Trace.Info($"User provided port: {port.Value}"); Trace.Info($"User provided port: {port.Value}");
} }
foreach (var volume in container.UserMountVolumes) foreach (var mount in container.UserMountVolumes)
{ {
Trace.Info($"User provided volume: {volume.Value}"); Trace.Info($"User provided volume: {mount.UserProvidedValue}");
var mount = new MountVolume(volume.Value);
if (string.Equals(mount.SourceVolumePath, "/", StringComparison.OrdinalIgnoreCase)) if (string.Equals(mount.SourceVolumePath, "/", StringComparison.OrdinalIgnoreCase))
{ {
executionContext.Warning($"Volume mount {volume.Value} is going to mount '/' into the container which may cause file ownership change in the entire file system and cause Actions Runner to lose permission to access the disk."); executionContext.Warning($"Volume mount {mount.UserProvidedValue} is going to mount '/' into the container which may cause file ownership change in the entire file system and cause Actions Runner to lose permission to access the disk.");
} }
} }