-
Notifications
You must be signed in to change notification settings - Fork 2
User ns reworked #4
Changes from all commits
f4e1796
c1386c3
d54cddc
dc7798d
31640cb
15111e3
ac6a9ac
8966f54
b14dc20
f2369a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -115,6 +115,8 @@ type Runtime interface { | |||||||||
| // This method just proxies a new runtimeConfig with the updated | ||||||||||
| // CIDR value down to the runtime shim. | ||||||||||
| UpdatePodCIDR(podCIDR string) error | ||||||||||
| // GetRuntimeConfigInfo returns runtime's configuration details, eg: if user-namespaces are enabled or not | ||||||||||
| GetRuntimeConfigInfo() (*RuntimeConfigInfo, error) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // StreamingRuntime is the interface implemented by runtimes that handle the serving of the | ||||||||||
|
|
@@ -422,12 +424,6 @@ type RunContainerOptions struct { | |||||||||
| ReadOnly bool | ||||||||||
| // hostname for pod containers | ||||||||||
| Hostname string | ||||||||||
| // EnableHostUserNamespace sets userns=host when users request host namespaces (pid, ipc, net), | ||||||||||
| // are using non-namespaced capabilities (mknod, sys_time, sys_module), the pod contains a privileged container, | ||||||||||
| // or using host path volumes. | ||||||||||
| // This should only be enabled when the container runtime is performing user remapping AND if the | ||||||||||
| // experimental behavior is desired. | ||||||||||
| EnableHostUserNamespace bool | ||||||||||
|
rata marked this conversation as resolved.
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| // VolumeInfo contains information about the volume. | ||||||||||
|
|
@@ -465,6 +461,68 @@ type RuntimeStatus struct { | |||||||||
| Conditions []RuntimeCondition | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // RuntimeConfigInfo contains runtime's configuration details, eg: user-namespaces mapping between host and container | ||||||||||
| type RuntimeConfigInfo struct { | ||||||||||
| UserNamespaceConfig UserNamespaceConfigInfo | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // UserNamespaceConfigInfo contains runtime's user-namespace configuration | ||||||||||
| type UserNamespaceConfigInfo struct { | ||||||||||
| UidMappings []*UserNSMapping | ||||||||||
| GidMappings []*UserNSMapping | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // UserNSMaping represents mapping of user-namespaces between host and container | ||||||||||
| type UserNSMapping struct { | ||||||||||
| ContainerID uint32 | ||||||||||
| HostID uint32 | ||||||||||
| Size uint32 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // IsUserNamespaceEnabled returns true if user-namespace feature is enabled at runtime | ||||||||||
| func (c *RuntimeConfigInfo) IsUserNamespaceEnabled() bool { | ||||||||||
| if len(c.UserNamespaceConfig.UidMappings) == 0 { | ||||||||||
| return false | ||||||||||
| } | ||||||||||
| if len(c.UserNamespaceConfig.UidMappings) == 1 && | ||||||||||
| c.UserNamespaceConfig.UidMappings[0].HostID == uint32(0) && c.UserNamespaceConfig.UidMappings[0].Size == uint32(4294967295) { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit picking: is the I mean, golang does force to use some explicit conversions, but as explained here I don't think it should be needed to compare a var with a constant. Am I missing something?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I'll remove them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4294967295 encapsulates the entire uid/gid range I believe. The initial root namespace has a uid_map of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the idea is that if the configured mapping is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh, I see. Thanks! Maybe a comment can make it very clear to everyone :)
Comment on lines
+487
to
+488
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be more clear.
Suggested change
|
||||||||||
| return false | ||||||||||
| } | ||||||||||
| return true | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // IsUserNamespaceSupported returns true if user-namespace feature is supported at runtime | ||||||||||
| func (c *RuntimeConfigInfo) IsUserNamespaceSupported() bool { | ||||||||||
| if len(c.UserNamespaceConfig.UidMappings) == 0 { | ||||||||||
| return false | ||||||||||
| } | ||||||||||
| if len(c.UserNamespaceConfig.UidMappings) == 1 && | ||||||||||
| c.UserNamespaceConfig.UidMappings[0].HostID == uint32(0) && c.UserNamespaceConfig.UidMappings[0].Size == uint32(0) { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem regarding
Comment on lines
+499
to
+500
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why if Is it just when the size is 0? Why does it matter, in that case, that the hostID is 0 too? Or why wouldn't it be a problem if the specified mappings are more than one?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's trying to detect the case where a mapping like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense |
||||||||||
| return false | ||||||||||
| } | ||||||||||
| return true | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // GetHostUIDFor returns uid on host usernamespace that is mapped to the given uid in container usernamespace | ||||||||||
| func (c *RuntimeConfigInfo) GetHostUIDFor(containerUID uint32) (uint32, error) { | ||||||||||
| for _, mapping := range c.UserNamespaceConfig.UidMappings { | ||||||||||
| if containerUID >= mapping.ContainerID && containerUID < mapping.ContainerID+mapping.Size { | ||||||||||
| return mapping.HostID + (containerUID - mapping.ContainerID), nil | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return 0, fmt.Errorf("IdMapping not found for container usernamespace UID %v", containerUID) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // GetHostGIDFor returns gid on host usernamespace that is mapped to the given gid in container usernamespace | ||||||||||
| func (c *RuntimeConfigInfo) GetHostGIDFor(containerGID uint32) (uint32, error) { | ||||||||||
| for _, mapping := range c.UserNamespaceConfig.GidMappings { | ||||||||||
| if containerGID >= mapping.ContainerID && containerGID < mapping.ContainerID+mapping.Size { | ||||||||||
| return mapping.HostID + (containerGID - mapping.ContainerID), nil | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return 0, fmt.Errorf("IdMapping not found for container usernamespace GID %v", containerGID) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // GetRuntimeCondition gets a specified runtime condition from the runtime status. | ||||||||||
| func (r *RuntimeStatus) GetRuntimeCondition(t RuntimeConditionType) *RuntimeCondition { | ||||||||||
| for i := range r.Conditions { | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a test with more than one mapping too, given that this is a slice?