Skip to content

Groups

Several Posit settings refer to Linux groups:

  • Workbench gives Admin Dashboard access to the groups named in admin-group and admin-superuser-group in rserver.conf.1
  • The Launcher admin-group option names the group of users who can see and control all jobs.2
  • Connect requires every Unix account used as a RunAs account to be a member of the group named by Applications.SharedRunAsUnixGroup, which defaults to rstudio-connect.3
  • Package Manager requires users of the local rspm CLI to be members of the rstudio-pm group.4

Shared file systems such as NFS enforce permissions by numeric UID and GID. Connect documents that the accounts and groups used to run content need the same IDs on every node.5

posit-groups

Lists the rstudio-server, rstudio-connect, and rstudio-pm groups; the groups named in auth-required-user-group, admin-group, and admin-superuser-group in rserver.conf, in admin-group in launcher.conf, and in Connect's Applications.SharedRunAsUnixGroup; and any names in POSIT_GROUPS. For each group it shows the GID and the members. It also shows whether the group is local (in /etc/group) or comes from a directory service such as LDAP or Active Directory. It then shows the rstudio-server, rstudio-connect, and rstudio-pm accounts with id. Everything comes from getent and id, so no root is needed.

posit-groups

--export FILE writes the same groups as name:gid:members lines to a new file, mode 0600. Copy the file to another server and run --plan FILE there. The plan prints the groupadd and usermod -aG commands that would make the local groups match the export. It runs nothing, but it notes:

  • A GID that differs. Changing the GID of an existing group does not change the group of files it already owns.
  • A GID that is already taken.
  • Members who do not exist on this server.
  • Groups that come from a directory service. Change these in the directory, not with groupadd.
posit-groups --export ~/groups-old-server.txt
posit-groups --plan ~/groups-old-server.txt

Review the plan, then send it to your sudo administrator.

The script needs posit-lib.sh in the same directory.

Source: scripts/posit-groups
#!/bin/bash
#
# posit-groups: report the Linux groups Posit uses, without root.
#
# Lists the service account groups, the groups named in the Workbench,
# Launcher, and Connect configuration, and any extra groups in POSIT_GROUPS,
# with GID, members, and whether each is a local group or comes from a
# directory service. It never changes groups: --plan prints the commands
# the sudo administrator would run to match an export from another server.
#
# Usage:
#   posit-groups                 report
#   posit-groups --export FILE   write name:gid:members lines to FILE
#   posit-groups --plan FILE     print commands to match FILE on this host
#

# Report scripts keep going when one check fails, so -e is not set
set -uo pipefail

# shellcheck source=posit-lib.sh
. "$(dirname "$(readlink -f "$0")")/posit-lib.sh" || exit 1

readonly WB_CONF_DIR="${POSIT_WB_CONF_DIR:-/etc/rstudio}"
readonly DEFAULT_CONNECT_CONF='/etc/rstudio-connect/rstudio-connect.gcfg'
readonly CONNECT_CONF="${POSIT_CONNECT_CONF:-${DEFAULT_CONNECT_CONF}}"
readonly ACCOUNTS=(rstudio-server rstudio-connect rstudio-pm)
readonly ROW_FORMAT='  %-28s %-8s %-10s %s\n'

#######################################
# Print the groups Posit uses, one per line, without duplicates.
# Globals:
#   ACCOUNTS
#   WB_CONF_DIR
#   CONNECT_CONF
#   POSIT_GROUPS (space separated list, optional)
#######################################
posit_groups() {
  local k
  local extra=()
  read -ra extra <<< "${POSIT_GROUPS:-}"
  {
    printf '%s\n' "${ACCOUNTS[@]}"
    for k in auth-required-user-group admin-group admin-superuser-group; do
      ini_value "${WB_CONF_DIR}/rserver.conf" "${k}" | tr ',' '\n'
    done
    ini_value "${WB_CONF_DIR}/launcher.conf" admin-group | tr ',' '\n'
    gcfg_value "${CONNECT_CONF}" Applications SharedRunAsUnixGroup
    printf '%s\n' "${extra[@]}"
  } | tr -d ' ' | awk 'NF && !seen[$0]++'
}

#######################################
# Print whether a group is local or comes from a directory service.
# Arguments:
#   Group name.
# Outputs:
#   "local" if the group is in /etc/group, "directory" otherwise.
#######################################
group_source() {
  if awk -F: -v g="$1" '$1 == g { found = 1 } END { exit !found }' \
    /etc/group; then
    echo local
  else
    echo directory
  fi
}

#######################################
# Print the groups report.
# Globals:
#   ACCOUNTS
#   ROW_FORMAT
#######################################
report() {
  local g
  local entry
  local name
  local gid
  local members
  local a
  report_start "Posit groups report"
  header "Groups"
  # The format string is a constant, so variables in it are intended
  # shellcheck disable=SC2059
  printf "${ROW_FORMAT}" "Group" "GID" "Source" "Members"
  while read -r g; do
    if entry="$(getent group -- "${g}")"; then
      IFS=: read -r name _ gid members <<< "${entry}"
      # shellcheck disable=SC2059
      printf "${ROW_FORMAT}" "${name}" "${gid}" "$(group_source "${name}")" \
        "${members:-none listed}"
    else
      printf '  %-28s %s\n' "${g}" "not found"
    fi
  done < <(posit_groups)
  note "a directory group (LDAP, Active Directory) may show no members" \
    "here; check with id USER"

  header "Service accounts"
  for a in "${ACCOUNTS[@]}"; do
    item "${a}" "$(id -- "${a}" 2>/dev/null || echo 'no such user')"
  done
}

#######################################
# Write name:gid:members lines for each group to a new file.
# Arguments:
#   File to create.
#######################################
export_groups() {
  local file="$1"
  local g
  if [[ -e "${file}" ]]; then
    echo "${file} already exists; choose a new name" >&2
    exit 1
  fi
  (
    umask 077
    while read -r g; do
      getent group -- "${g}" | awk -F: '{ print $1 ":" $3 ":" $4 }'
    done < <(posit_groups) > "${file}"
  )
  echo "Wrote $(wc -l < "${file}") groups to ${file}"
}

#######################################
# Print the commands that would match an export file on this host.
# Nothing is changed.
# Globals:
#   ROOT_ADMIN
# Arguments:
#   Export file from --export.
#######################################
plan() {
  local file="$1"
  local name
  local gid
  local members
  local cur
  local cur_gid
  local cur_members
  local m
  local member_list
  local orphan='changing it orphans files owned by GID'
  if [[ ! -r "${file}" ]]; then
    echo "Cannot read ${file}" >&2
    exit 1
  fi
  echo "# Commands to match ${file} on $(hostname -f 2>/dev/null || hostname)"
  echo "# Nothing has been changed. Review, then ask ${ROOT_ADMIN} to run them."
  while IFS=: read -r name gid members || [[ -n "${name}" ]]; do
    if ! [[ "${name}" =~ ^[a-z_][a-z0-9_-]*$ && "${gid}" =~ ^[0-9]+$ ]]; then
      echo "# skipped malformed line: ${name}"
      continue
    fi
    if ! cur="$(getent group -- "${name}")"; then
      if getent group "${gid}" >/dev/null; then
        echo "# GID ${gid} is taken here; let the system choose one"
        echo "sudo groupadd -- ${name}"
      else
        echo "sudo groupadd -g ${gid} -- ${name}"
      fi
      cur_members=""
    else
      IFS=: read -r _ _ cur_gid cur_members <<< "${cur}"
      if [[ "$(group_source "${name}")" == directory ]]; then
        echo "# ${name} comes from the directory service; ask the directory" \
          "team about members and GID"
        continue
      fi
      if [[ "${cur_gid}" != "${gid}" ]]; then
        echo "# ${name} has GID ${cur_gid} here and ${gid} in the export;" \
          "${orphan} ${cur_gid}"
      fi
    fi
    IFS=, read -ra member_list <<< "${members}"
    for m in "${member_list[@]}"; do
      if ! id -- "${m}" >/dev/null 2>&1; then
        echo "# user ${m} does not exist here"
      elif [[ ",${cur_members}," != *",${m},"* ]]; then
        echo "sudo usermod -aG ${name} -- ${m}"
      fi
    done
  done < "${file}"
}

main() {
  case "${1:-}" in
    "") report ;;
    --export) export_groups "${2:?--export needs a file name}" ;;
    --plan) plan "${2:?--plan needs an export file}" ;;
    -h|--help) sed -n '3,15p' "$0" | sed 's/^# \{0,1\}//' ;;
    *)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
  esac
}

main "$@"