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

Implemented chown file permissions and user/group creation #84

Merged
merged 1 commit into from
Nov 25, 2013
Merged
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
@@ -0,0 +1 @@
chown ${{user}}:${{group}} ${{path}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Adding ${{group}} as system group
if ! getent group ${{group}} > /dev/null 2>&1
then
echo "Creating system group: ${{group}}"
addgroup --system ${{group}}
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Adding ${{user}}
if ! id -u $1 >/dev/null 2>&1; then
echo "Creating user ${{user}} in group ${{group}}"
useradd --system --no-create-home --gid ${{group}} --shell /bin/false ${{user}}
fi
14 changes: 14 additions & 0 deletions src/main/resources/com/typesafe/sbt/packager/debian/postrm-purge
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Deleting user: ${{user}} and group: ${{group}}
case "$1" in
remove|failed-upgrade|abort-upgrade|abort-install|disappear)
;;
purge)
deluser --quiet --system ${{user}} > /dev/null || true
delgroup --quiet --system ${{group}} > /dev/null || true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure we can delete the group always?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Actually we don't have any clue about the user/group.
I think we have two options

  1. We remove this behavior. This will leave traces of the application after purging.
  2. Make this behavior configurable.

For 2. this would need a setting like

debianRemoveUser := true

debianRemoveGroup := true

and question is: What are the default values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm.... Well for now maybe we'll just document that it has to be a unique user for this service. That's generally a standard good practice. However, it does mean multiple services that are supposed to have the same user won't quite work. In that case, you want a DEB which adds the user/group, and then depend on that I think. In that case, you're probably directly implementing things.

SO, I'd say how it is now is fine for now.

;;
upgrade)
;;
*)
echo "postinst called with unknown argument \`\$1'" >&2
;;
esac
37 changes: 35 additions & 2 deletions src/main/scala/com/typesafe/sbt/packager/debian/DebianPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
chmod(cfile, "0644")
cfile
},
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target)
map { (mappings, _, maintScripts, _, replacements, symlinks, t) =>
debianExplodedPackage <<= (linuxPackageMappings, debianControlFile, debianMaintainerScripts, debianConffilesFile, debianControlScriptsReplacements, linuxPackageSymlinks, target, streams)
map { (mappings, _, maintScripts, _, replacements, symlinks, t, streams) =>

// Create files and directories
mappings foreach {
Expand Down Expand Up @@ -142,6 +142,32 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
filterAndFixPerms(targetFile, replacements, LinuxFileMetaData())
}

// Check for non root user/group and append to postinst / postrm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe this entire bit of behavior belongs in its own task/method, and we should figure out how to explode the package, then run this. Right now, this code is getting to be a bit large to "grok" all at once, and a handy method name can help. I can do this after merging if you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree. This was the first "it works" attempt :)
However I may hand this to you and will learn-by-reading this time.

// filter all root mappings, map to (user,group) key, group by, append everything
mappings filter {
case LinuxPackageMapping(_, LinuxFileMetaData("root", "root", _, _, _), _) => false
case _ => true
} map {
case LinuxPackageMapping(paths, LinuxFileMetaData(user, group, _, _, _), _) => (user, group) -> paths
} groupBy (_._1) foreach {
case ((user, group), pathList) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting and slick. Nice work.

streams.log info ("Altering postrm/postinst files to add user " + user + " and group " + group)
val postinst = t / "DEBIAN" / "postinst"
val postrm = t / "DEBIAN" / "postrm"

val replacements = Seq("group" -> group, "user" -> user)
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstGroupaddTemplateSource, replacements))
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstUseraddTemplateSource, replacements))

// remove key, flatten it and then go through each file
pathList.map(_._2).flatten foreach {
case (_, target) =>
val pathReplacements = replacements :+ ("path" -> target.toString)
IO.append(postinst, TemplateWriter.generateScript(DebianPlugin.postinstChownTemplateSource, pathReplacements))
}

IO.append(postrm, TemplateWriter.generateScript(DebianPlugin.postrmPurgeTemplateSource, replacements))
}
t
},
debianMD5sumsFile <<= (debianExplodedPackage, target) map {
Expand Down Expand Up @@ -180,3 +206,10 @@ trait DebianPlugin extends Plugin with linux.LinuxPlugin {
}))

}

object DebianPlugin {
private def postinstGroupaddTemplateSource: java.net.URL = getClass.getResource("postinst-groupadd")
private def postinstUseraddTemplateSource: java.net.URL = getClass.getResource("postinst-useradd")
private def postinstChownTemplateSource: java.net.URL = getClass.getResource("postinst-chown")
private def postrmPurgeTemplateSource: java.net.URL = getClass.getResource("postrm-purge")
}