-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate unique UIDs and GIDs (#663)
These changes have already partly been reviewed in canonical/authd-private#9. Closes #509 UDENG-5416 UDENG-4352
- Loading branch information
Showing
134 changed files
with
2,556 additions
and
592 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...al/brokers/testdata/golden/TestIsAuthenticated/Error_when_broker_returns_invalid_userinfo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FIRST CALL: | ||
access: | ||
data: | ||
err: message is not JSON formatted: json: cannot unmarshal string into Go value of type brokers.userInfo | ||
err: message is not JSON formatted: json: cannot unmarshal string into Go value of type types.UserInfo |
4 changes: 0 additions & 4 deletions
4
...rs/testdata/golden/TestIsAuthenticated/Error_when_broker_returns_userinfo_with_empty_UUID
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...l/brokers/testdata/golden/TestUnmarshalUserInfo/Successfully_unmarshal_complete_user_info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"Name":"success","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}],"UUID":""} | ||
{"Name":"success","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}]} |
2 changes: 1 addition & 1 deletion
2
.../testdata/golden/TestUnmarshalUserInfo/Unmarshaling_json_with_additional_field_ignores_it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"Name":"success","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}],"UUID":""} | ||
{"Name":"success","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}]} |
2 changes: 1 addition & 1 deletion
2
.../testdata/golden/TestUnmarshalUserInfo/Unmarshaling_json_with_empty_field_keeps_its_value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"Name":"","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}],"UUID":""} | ||
{"Name":"","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}]} |
2 changes: 1 addition & 1 deletion
2
...estdata/golden/TestUnmarshalUserInfo/Unmarshaling_json_with_missing_field_adds_zero_value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"Name":"","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}],"UUID":""} | ||
{"Name":"","UID":82162,"Gecos":"gecos for success","Dir":"/home/success","Shell":"/bin/sh/success","Groups":[{"Name":"success","GID":82162,"UGID":""},{"Name":"group-success","GID":81868,"UGID":""}]} |
2 changes: 1 addition & 1 deletion
2
internal/brokers/testdata/golden/TestUserPreCheck/Successfully_pre-check_user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Package errno provide utilities to use C errno from the Go side. | ||
package errno | ||
|
||
/* | ||
#include <errno.h> | ||
#include <string.h> | ||
static void unset_errno(void) { | ||
errno = 0; | ||
} | ||
static int get_errno(void) { | ||
return errno; | ||
} | ||
static void set_errno(int e) { | ||
errno = e; | ||
} | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
// Error is the type for the errno error. | ||
type Error C.int | ||
|
||
func (errno Error) Error() string { | ||
return C.GoString(C.strerror(C.int(errno))) | ||
} | ||
|
||
const ( | ||
// ErrNoEnt is the errno ENOENT. | ||
ErrNoEnt Error = C.ENOENT | ||
// ErrSrch is the errno ESRCH. | ||
ErrSrch Error = C.ESRCH | ||
// ErrBadf is the errno EBADF. | ||
ErrBadf Error = C.EBADF | ||
// ErrPerm is the errno EPERM. | ||
ErrPerm Error = C.EPERM | ||
) | ||
|
||
// All these functions are expected to be called while this mutex is locked. | ||
var mu sync.Mutex | ||
|
||
// Lock the usage of errno. | ||
func Lock() { | ||
mu.Lock() | ||
C.unset_errno() | ||
} | ||
|
||
// Unlock unlocks the errno package for being re-used. | ||
func Unlock() { | ||
C.unset_errno() | ||
mu.Unlock() | ||
} | ||
|
||
// Get gets the current errno as [Error]. | ||
func Get() error { | ||
if mu.TryLock() { | ||
mu.Unlock() | ||
panic("Using errno without locking!") | ||
} | ||
if errno := C.get_errno(); errno != 0 { | ||
return Error(errno) | ||
} | ||
return nil | ||
} | ||
|
||
func set(err error) { | ||
if mu.TryLock() { | ||
mu.Unlock() | ||
panic("Using errno without locking!") | ||
} | ||
|
||
var e Error | ||
if err != nil && !errors.As(err, &e) { | ||
panic("Not a valid errno value") | ||
} | ||
C.set_errno(C.int(e)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package errno | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNoError(t *testing.T) { | ||
t.Parallel() | ||
|
||
Lock() | ||
defer Unlock() | ||
|
||
require.NoError(t, Get()) | ||
} | ||
|
||
func TestGetWithoutLocking(t *testing.T) { | ||
// This test can't be parallel, since other tests may locking meanwhile. | ||
|
||
require.PanicsWithValue(t, "Using errno without locking!", func() { _ = Get() }) | ||
} | ||
|
||
func TestSetWithoutLocking(t *testing.T) { | ||
// This test can't be parallel, since other tests may locking meanwhile. | ||
|
||
require.PanicsWithValue(t, "Using errno without locking!", func() { set(nil) }) | ||
} | ||
|
||
func TestSetInvalidError(t *testing.T) { | ||
t.Parallel() | ||
|
||
Lock() | ||
defer Unlock() | ||
|
||
require.PanicsWithValue(t, "Not a valid errno value", func() { set(errors.New("invalid")) }) | ||
} | ||
|
||
func TestErrorValues(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
err error | ||
}{ | ||
"No error": {}, | ||
"No such file or directory": {err: ErrNoEnt}, | ||
"No such process": {err: ErrSrch}, | ||
"Bad file descriptor": {err: ErrBadf}, | ||
"Operation not permitted": {err: ErrPerm}, | ||
} | ||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
Lock() | ||
defer Unlock() | ||
|
||
set(tc.err) | ||
t.Logf("Checking for error %v", tc.err) | ||
require.ErrorIs(t, Get(), tc.err, "Errorno is not matching") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/services/nss/testdata/golden/TestGetGroupByGID/Return_existing_group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: group1 | ||
passwd: x | ||
passwd: "" | ||
gid: 11111 | ||
members: | ||
- user1 |
2 changes: 1 addition & 1 deletion
2
internal/services/nss/testdata/golden/TestGetGroupByName/Return_existing_group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: group1 | ||
passwd: x | ||
passwd: "" | ||
gid: 11111 | ||
members: | ||
- user1 |
Oops, something went wrong.