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

Add explicit setuid support for RPM and Deb tasks #446

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,15 @@ class DebCopyAction extends AbstractPackagingCopyAction<Deb> {
Integer uid = (Integer) lookup(specToLookAt, 'uid') ?: task.uid ?: 0
String group = lookup(specToLookAt, 'permissionGroup') ?: task.permissionGroup
Integer gid = (Integer) lookup(specToLookAt, 'gid') ?: task.gid ?: 0
Boolean setuid = lookup(specToLookAt, 'setuid')

int fileMode = FilePermissionUtil.getUnixPermission(fileDetails)
if (setuid == null) {
setuid = task.setuid
}
if (setuid) {
fileMode = fileMode | 04000
}

debFileVisitorStrategy.addFile(fileDetails, inputFile, user, uid, group, gid, fileMode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class CopySpecEnhancement {
setgid(spec, setgid)
}

static void setuid(CopySpec spec, boolean setuid) {
appendFieldToCopySpec(spec, 'setuid', setuid)
}

static void setSetuid(CopySpec spec, boolean setuid) {
setuid(spec, setuid)
}

static void permissionGroup(CopySpec spec, String permissionGroup) {
appendFieldToCopySpec(spec, 'permissionGroup', permissionGroup)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SystemPackagingExtension {
String user
String permissionGroup // Group is used by Gradle on tasks.
boolean setgid
boolean setuid

/**
* In Debian, this is the Section and has to be provided. Valid values are: admin, cli-mono, comm, database, debug,
Expand Down Expand Up @@ -188,6 +189,12 @@ class SystemPackagingExtension {
return setgid
}

@Input
@Optional
Boolean getSetuid() {
return setuid
}

@Input
@Optional
String getPackageGroup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ abstract class SystemPackagingTask extends OsPackageAbstractArchiveTask {
mapping.map('uploaders', { parentExten?.getUploaders() ?: getPackager() })
mapping.map('permissionGroup', { parentExten?.getPermissionGroup() ?: '' })
mapping.map('setgid', { parentExten?.getSetgid() ?: false })
mapping.map('setuid', { parentExten?.getSetuid() ?: false })
mapping.map('packageGroup', { parentExten?.getPackageGroup() })
mapping.map('buildHost', { parentExten?.getBuildHost() ?: HOST_NAME })
mapping.map('summary', { parentExten?.getSummary() ?: getPackageName() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class RpmCopyAction extends AbstractPackagingCopyAction<Rpm> {
String group = lookup(specToLookAt, 'permissionGroup') ?: task.permissionGroup

int fileMode = FilePermissionUtil.getFileMode(specToLookAt) ?: FilePermissionUtil.getUnixPermission(fileDetails)
Boolean setuid = lookup(specToLookAt, 'setuid')
if (setuid == null) {
setuid = task.setuid
}
if (setuid) {
fileMode = fileMode | 04000
}
def specAddParentsDir = lookup(specToLookAt, 'addParentDirs')
boolean addParentsDir = specAddParentsDir != null ? specAddParentsDir : task.addParentDirs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,92 @@ buildRpm {
true | 1444
false | 0644
}

def 'setuid can be set in rpm and deb'() {
given:
File bananaFile = new File(projectDir, 'test/banana')
FileUtils.forceMkdirParent(bananaFile)
bananaFile.text = 'banana'
buildFile << """
plugins {
id 'com.netflix.nebula.ospackage'
}

version = '1.0.0'

ospackage {
addParentDirs false
}

buildRpm {
packageName = 'sample'

from(${GradleUtils.quotedIfPresent(bananaFile.getParentFile().path)}) {
setuid = ${setuidValue}
filePermissions {
unix(0755)
}
into '/usr/share/myproduct/etc/'
}
}
"""
when:
runTasks('buildRpm')

then:
def scanFiles = Scanner.scan(file('build/distributions/sample-1.0.0.noarch.rpm')).files

['./usr/share/myproduct/etc/banana'] == scanFiles*.name
[FILE] == scanFiles*.type
[expectedPermissions] == scanFiles*.permissions

where:
setuidValue | expectedPermissions
true | 04755
false | 00755
}

def 'setgid in ospackage extension propagates to rpm and deb'() {
given:
File bananaFile = new File(projectDir, 'test/banana')
FileUtils.forceMkdirParent(bananaFile)
bananaFile.text = 'banana'
buildFile << """
plugins {
id 'com.netflix.nebula.ospackage'
}

version = '1.0.0'

ospackage {
addParentDirs false
setuid = ${setuidValue}
}

buildRpm {
packageName = 'sample'

from(${GradleUtils.quotedIfPresent(bananaFile.getParentFile().path)}) {
filePermissions {
unix(0755)
}
into '/usr/share/myproduct/etc/'
}
}
"""
when:
runTasks('buildRpm', '--warning-mode', 'all', '--stacktrace')

then:
def scanFiles = Scanner.scan(file('build/distributions/sample-1.0.0.noarch.rpm')).files

['./usr/share/myproduct/etc/banana'] == scanFiles*.name
[FILE] == scanFiles*.type
[expectedPermissions] == scanFiles*.permissions

where:
setuidValue | expectedPermissions
true | 04755
false | 00755
}
}