47 Commits

Author SHA1 Message Date
Philip Meulengracht
13a1c0ca10 multiple: add a spread test for the auto-removal of expired system-users. Make use of --force when removing users due to expiration, otherwise it is possible to block the removal the user if the user left a process open. 2022-10-31 14:05:48 +01:00
Philip Meulengracht
c86536d5a1 daemon: use a counter instead of a bool to detect the calls to CreateUser 2022-10-21 09:04:41 +02:00
Philip Meulengracht
2cba9bacce daemon: use the equal function of the timestamp instead 2022-10-20 13:37:57 +02:00
Philip Meulengracht
24437a0c4a daemon: support for expiration date in REST API 2022-10-20 12:14:40 +02:00
Philip Meulengracht
0dde00cdbd o/auth: rename NewUserData to NewUserParams 2022-10-04 13:35:45 +02:00
Ondrej
a044081e73 daemon: move user add, remove operations to overlord device state (#11796)
* daemon,o/devicestate: move user creation and removal helpers to o/devicestate

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon,o/devicestate: move user create,remove tests to o/devicestate

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon: add new tests for user create,remove requests

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* o/devicestate: removed unused variable in users test

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* o/devicestate: move users test export to common export

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* o/devicestate: remove left behind commented code

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* o/devicestate: clean syntax in user helper

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon: use testutil.Backup() in api export tests

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* o/devicestate: use testutil.Backup() in api export tests

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon: cleanup user tests

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: join helper variables for mocking

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: remove extra line in addUser function

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestat: rename internal_err -> internalErr

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: change addUser fnc signature

addUser adds single user. Change function signarure to return single UserResponse
Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: update error handling in CreateUser

Update error handling after merge from master

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: remove accidental file from merge conflict

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon,overlord: user error wrapper for user account actions

CreateUser, RemoveUser can fail for multiple reasons.
There is a need to distinguish between internal error and bad request.
Use UserError structure to wrap/ return error information.

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon,overlord: address review issues

- renaming UserResponse to CreatedUser
- fix typos
- fix error wrapping
- rename ue to error
- code cleanup

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: rework createUserOpts helper for user creation

- remove unused and confusing safe flag for create user operations
- remove state from struct and pass it as function argument

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate: add test for missing email in user creation

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* daemon,overlord: split CreateUser function for known and unknown users

Split CreateUser functionality into two new functions
- CreateKnownUsers for creation of known users
- CreateUser for creation of user defined by email

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord: use new auth.NewUserData structure

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>

* overlord/devicestate,daemon: review feedback

Reorder functions, drop wrapping internal errors, assume state lock on function entry to match the style in managers

* daemon,overlord/devicestate: review feedback

add missing unit test, remove devicemanager parameter, fix doc string

* daemon: review feedback

simplify some else conditions, rename function and variable

* overlord/devicestate: review feedback

move createUser down to the structure it belongs to

* daemon: review feedback + changes to how we return users

simplify a lot of the methods, change to pointer instead so we can return nil's instead of empty structures. Remove the option structure for creating new users, update some docs

* daemon: review feedback

rename doUserWrapper/doCreateUser

* daemon: restore tests, restore the backwards compatible way of creating users

* daemon,overlord: review feedback

rewrite testPostCreateUserFromAssertion, it was no longer valid after code seperation, instead focus on testing the code in api_users.go
update checks in users_test.go, one was invalid (overwritten), rest was missing verification of calling

* daemon,overlord/devicestate: review feedback

redo some of the error messages, move the logic check for creating known users in compat-mode, update comments a bit

Signed-off-by: Ondrej Kubik <ondrej.kubik@canonical.com>
Co-authored-by: Philip Meulengracht <the_meulengracht@hotmail.com>
2022-09-30 09:33:37 +02:00
Philip Meulengracht
aa5b512e0a multiple: refactor arguments given to NewUser into a struct instead 2022-08-26 11:14:16 +02:00
Michael Vogt
718717e5c5 daemon: make marker clearer (thanks to Samuele) 2021-11-22 13:19:02 +01:00
Michael Vogt
47b7888897 daemon: amend ssh keys coming from the store
This commit adds a comment to ssh keys writen to ~/.authorized_keys
that come from the store. This will enable us in the future to
update keys that come from the store because we now have the
information what keys got added by snapd and which were added by
other means.
2021-09-10 17:31:54 +02:00
Samuele Pedroni
33245574ad daemon: drop apiBaseSuite.apiErrorReq as now errorReq is the same 2021-06-10 13:15:35 +02:00
Samuele Pedroni
c9d961a863 daemon: have apiBaseSuite.errorReq return *apiError directly
now that apiError is used everywhere to return errors

result naming and checks in tests are adjusted accordingly
2021-06-10 13:15:35 +02:00
Samuele Pedroni
7a84ff7d01 daemon: switch api_users.go to apiError, also cover related paths
this also adds some unit tests to cover some of the touched code paths
that weren't tested before
2021-06-01 18:48:43 +02:00
Samuele Pedroni
466f6206e0 daemon: use rspe (response error) instead of very contracted ae (api error) 2021-06-01 14:22:26 +02:00
Samuele Pedroni
082ec321aa daemon: start using apiError instead of building resp(JSON) directly
adjust tests as necessary, in most cases the test are simpler as
e.g. they need less casting
2021-05-28 19:48:20 +02:00
Samuele Pedroni
7488b81a72 daemon: force to express what kind of access check is expected
have a family fo apiBaseSuite.expect*Access methods for this

the default expectation is read=open,write=authenticated

maybe we want something different
2021-05-19 18:09:06 +02:00
Samuele Pedroni
8493ed9ebe daemon: streamline setup for auhtorized/unauthorized requests
add apiBaseSuite.asRootAuth/asUserAuth taking an *http.Request to
enhance with auth info

clean up some ineffective setup
2021-05-19 18:09:06 +02:00
James Henstridge
eebe1636b2 Merge remote-tracking branch 'upstream/master' into daemon-access-check 2021-04-07 17:02:42 +08:00
Samuele Pedroni
7aa0362582 daemon: introduce apiBaseSuite.(json|sync|async|error)Req
these test a daemon request and check the expected response type
before returning the response for further specific checks

this simplifies away some checks in the tests but the bigger win is
avoiding the .(*daemon.Resp) casts everywhere, in particular
this will reduce the number of affected places if we change
somewhat the internal types used to produce responses
2021-03-31 17:26:49 +02:00
Samuele Pedroni
8a2c156231 Merge remote-tracking branch 'upstream/master' into daemon-cleanup-api-tests-21 2021-03-29 10:27:27 +02:00
Maciej Borzecki
b9db71e511 daemon: fix signing key validity timestamp in unit tests
Occasionally in the Github test runs we observe this problem:

----------------------------------------------------------------------
PANIC: api_users_test.go:1074: userSuite.TestPostCreateUserFromAssertionNoModel

... Panic: cannot add test assertions: model assertion timestamp "2021-03-23 22:31:21 +0000 UTC" outside of signing key validity (key valid since "2021-03-23 22:31:22 +0000 UTC") (PC=0x439C58)

/snap/go/7221/src/runtime/panic.go:965
  in gopanic
/home/runner/work/snapd/snapd/src/github.com/snapcore/snapd/overlord/assertstate/assertstatetest/add_many.go:38
  in AddMany
api_users_test.go:1088
  in userSuite.TestPostCreateUserFromAssertionNoModel
/snap/go/7221/src/reflect/value.go:337
  in Value.Call
/snap/go/7221/src/runtime/asm_amd64.s:1371
  in goexit
OOPS: 444 passed, 1 PANICKED
--- FAIL: Test (11.20s)
FAIL
FAIL	github.com/snapcore/snapd/daemon	11.374s

The offending tests generates the model of a mocked my-brand whic gets sine with
they brand key, before the account key assertion of a given brand is generated.
If the code happens to run at the second boundary, it is possible that the model
timestamp will be before the account key assertion timestamp.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-03-24 08:47:11 +01:00
Samuele Pedroni
e1f7348ed3 daemon: move TestUsersOnlyRoot, TestLogsNoServices to the right suites
make also the necessary adjustment/simplifications
2021-03-16 11:42:38 +01:00
Samuele Pedroni
2de27f7974 daemon: move /v2/login|logout tests to api_users_test.go
some bits of apiBaseTest can also be moved as they are strictly used
only for these
2021-03-10 18:49:23 +01:00
Samuele Pedroni
e43f0eda4f daemon: comments to clarify test mechanics 2021-03-02 09:17:56 +01:00
Samuele Pedroni
4567f6ec37 daemon,o/c/configcore: introduce users.create.automatic
the option if set to false disables automatic user creation on assertion
auto-import

it is processed early which means its setting available
before snapd starts serving users API requests
2021-03-01 17:06:18 +01:00
Samuele Pedroni
d74296ef67 daemon: convert tests using daemon.APIBaseSuite to apiBaseSuite 2020-12-07 16:51:56 +01:00