Skip to content

Commit

Permalink
many: do not use nss when looking up for users/groups from snapd snap (
Browse files Browse the repository at this point in the history
…#13776)

* many: do not use runtime nss when looking up for users/groups from snapd snap

When snapd runs as a snap, it has its own runtime. This may not have
NSS plugins needed for the host. For example to get users from
AD/LDAP/Kerberos, or systemd-homed, or custom user databses.  In
general we can use tag `osusergo` to make go not to use the local
configuration (i.e. `/etc/nsswitch.conf`), however, even if it is fine
for most databases, we really need users and groups to be resolved
with the host configuration.

To be able to load correctly plugins, we expect the host system to
provide `getent`. And we query `passwd` and `group` databases through
this command.

In the future we should connect the systemd-userdb if it is
running and use `getent` only as fallback.

* .golangci.yml: verify for forbidden use of os/user
  • Loading branch information
valentindavid authored Sep 30, 2024
1 parent d7f8099 commit 15296f9
Show file tree
Hide file tree
Showing 58 changed files with 564 additions and 61 deletions.
14 changes: 9 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,15 @@ linters-settings:
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 3
# depguard:
# list-type: blacklist
# include-go-root: false
# packages:
# - github.com/davecgh/go-spew/spew
depguard:
rules:
osuser:
files:
- "!**/osutil/user/*.go"
deny:
- pkg: "os/user"
desc: "Please use osutil/user instead. See https://github.com/canonical/snapd/pull/13776"

misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
Expand Down
6 changes: 2 additions & 4 deletions build-aux/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ parts:
- squashfs-tools
- xdelta3
- zlib1g
# This is needed for using os/user on Ubuntu Core
# TODO: do not use os/user, but io.systemd.NameServiceSwitch through dbus
- libnss-extrausers
stage:
- -usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/ld*.so*
- -lib32
Expand All @@ -125,9 +122,9 @@ parts:
- -usr/share/man
- -usr/share/lintian
- -usr/share/lintian/**
- -usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libnss_*.so.2
override-build: |
craftctl default
mv "${CRAFT_PART_INSTALL}/usr/lib/libnss_extrausers.so.2" "${CRAFT_PART_INSTALL}/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}/"
cp -rT "${CRAFT_PART_INSTALL}/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}" "${CRAFT_PART_INSTALL}/usr/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}"
rm -rf "${CRAFT_PART_INSTALL}/lib/${CRAFT_ARCH_TRIPLET_BUILD_FOR}"
rm -f "${CRAFT_PART_INSTALL}/lib/${DYNAMIC_LINKER}"
Expand Down Expand Up @@ -370,6 +367,7 @@ parts:
esac
;;
esac
TAGS+=(snapdusergo osusergo)
# FIPS specific build tags
if [ -f fips-build ]; then
Expand Down
2 changes: 1 addition & 1 deletion client/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
"errors"
"fmt"
"net/url"
"os/user"
"strconv"
"strings"
"time"

"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/snap"
)

Expand Down
2 changes: 1 addition & 1 deletion client/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ package client_test
import (
"encoding/json"
"fmt"
"os/user"
"strconv"
"strings"

"gopkg.in/check.v1"

"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/osutil/user"
)

func mksvc(snap, app string) *client.AppInfo {
Expand Down
2 changes: 1 addition & 1 deletion client/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"encoding/json"
"fmt"
"os"
"os/user"
"path/filepath"

"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/sys"
"github.com/snapcore/snapd/osutil/user"
)

// User holds logged in user information.
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_routine_file_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import (
"fmt"
"net/http"
"net/url"
"os/user"
"path/filepath"
"strings"

. "gopkg.in/check.v1"

"github.com/snapcore/snapd/client"
snap "github.com/snapcore/snapd/cmd/snap"
"github.com/snapcore/snapd/osutil/user"
)

type SnapRoutineFileAccessSuite struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strconv"
Expand All @@ -49,6 +48,7 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/strace"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/sandbox/cgroup"
"github.com/snapcore/snapd/sandbox/selinux"
"github.com/snapcore/snapd/snap"
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"time"
Expand All @@ -40,6 +39,7 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/strace"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/sandbox/cgroup"
"github.com/snapcore/snapd/sandbox/selinux"
"github.com/snapcore/snapd/snap"
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ package main
import (
"errors"
"fmt"
"os/user"
"strconv"

"github.com/jessevdk/go-flags"

"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/osutil/user"
)

type svcStatus struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"os/user"
"sort"
"strings"
"time"
Expand All @@ -32,6 +31,7 @@ import (

"github.com/snapcore/snapd/client"
snap "github.com/snapcore/snapd/cmd/snap"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/strutil"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/cmd_userd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net"
"net/http"
"os"
"os/user"
"path"
"path/filepath"
"strings"
Expand All @@ -38,6 +37,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/usersession/autostart"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"fmt"
"go/doc"
"os"
"os/user"
"strings"
"text/tabwriter"

Expand All @@ -35,6 +34,7 @@ import (
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/snap/channel"
"github.com/snapcore/snapd/strutil"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/snap/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"context"
"os"
"os/user"
"time"

"github.com/jessevdk/go-flags"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/snapcore/snapd/cmd/snaplock/runinhibit"
"github.com/snapcore/snapd/image"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/sandbox/cgroup"
"github.com/snapcore/snapd/sandbox/selinux"
"github.com/snapcore/snapd/seed/seedwriter"
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
"io"
"net/http"
"net/url"
"os/user"
"sort"
"strconv"
"strings"

"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/state"
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"math"
"net/http"
"net/http/httptest"
"os/user"
"sort"
"strconv"
"strings"
Expand All @@ -39,6 +38,7 @@ import (
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/snapstate"
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"fmt"
"net/http"
"os"
"os/user"
"path/filepath"
"time"

Expand All @@ -41,6 +40,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces/ifacetest"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/assertstate/assertstatetest"
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"fmt"
"net/http"
"os/user"
"time"

"gopkg.in/check.v1"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/snapcore/snapd/asserts/assertstest"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord/assertstate/assertstatetest"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/configstate/config"
Expand Down
3 changes: 1 addition & 2 deletions daemon/export_api_apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
package daemon

import (
"os/user"

"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/state"
Expand Down
2 changes: 1 addition & 1 deletion daemon/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ package daemon
import (
"context"
"net/http"
"os/user"
"time"

"github.com/gorilla/mux"

"github.com/snapcore/snapd/asserts/snapasserts"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/overlord"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/restart"
Expand Down
2 changes: 1 addition & 1 deletion desktop/portal/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ package portal

import (
"fmt"
"os/user"
"path/filepath"
"strings"

"github.com/snapcore/snapd/dbusutil"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/osutil/user"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion desktop/portal/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ package portal_test
import (
"errors"
"os"
"os/user"
"path/filepath"
"sync"

"github.com/godbus/dbus"
. "gopkg.in/check.v1"

"github.com/snapcore/snapd/desktop/portal"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/testutil"
)

Expand Down
2 changes: 1 addition & 1 deletion desktop/portal/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
package portal

import (
"os/user"
"time"

"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil/user"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion osutil/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"io"
"os"
"os/exec"
"os/user"
"syscall"
"time"

"github.com/snapcore/snapd/osutil/sys"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/testutil"
)
Expand Down
3 changes: 2 additions & 1 deletion osutil/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ package osutil
import (
"bytes"
"fmt"
"os/user"
"strconv"

"github.com/snapcore/snapd/osutil/user"
)

// FindUid returns the identifier of the given UNIX user name. It will
Expand Down
2 changes: 1 addition & 1 deletion osutil/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ package osutil_test

import (
"fmt"
"os/user"

"gopkg.in/check.v1"

"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/user"
"github.com/snapcore/snapd/testutil"
)

Expand Down
Loading

0 comments on commit 15296f9

Please sign in to comment.