blob: 526bcf1713c780c821a262b2ea315e41fdde68f3 [file] [log] [blame]
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package policy
import (
"context"
"time"
"github.com/golang/protobuf/ptypes/empty"
"go.chromium.org/tast-tests/cros/common/policy"
"go.chromium.org/tast-tests/cros/common/tape"
"go.chromium.org/tast-tests/cros/remote/gaiaenrollment"
"go.chromium.org/tast-tests/cros/remote/policyutil"
"go.chromium.org/tast-tests/cros/services/cros/graphics"
pspb "go.chromium.org/tast-tests/cros/services/cros/policy"
"go.chromium.org/tast/core/ctxutil"
"go.chromium.org/tast/core/rpc"
"go.chromium.org/tast/core/testing"
)
const samlEnrollmentTimeout = 7 * time.Minute
func init() {
testing.AddTest(&testing.Test{
Func: SAMLEnrollment,
LacrosStatus: testing.LacrosVariantUnneeded,
Desc: "SAML Enroll a device without checking policies",
Contacts: []string{
"cros-3pidp@google.com",
"lmasopust@google.com",
},
Attr: []string{"group:dmserver-enrollment-daily"},
SoftwareDeps: []string{"reboot", "chrome"},
ServiceDeps: []string{
"tast.cros.hwsec.OwnershipService",
"tast.cros.policy.PolicyService",
"tast.cros.tape.Service",
"tast.cros.graphics.ScreenshotService",
},
Timeout: samlEnrollmentTimeout,
Params: []testing.Param{
{
Name: "autopush",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerAlphaURL,
PoolID: tape.EnrollmentSAML,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerAlpha",
}},
},
{
Name: "staging",
Val: gaiaenrollment.TestParams{
DMServer: policy.DMServerStagingURL,
PoolID: tape.EnrollmentSAML,
},
// TODO b/346725308 Refactor to use utility and known dependency list.
ExtraSearchFlags: []*testing.StringPair{{
Key: "external_dependency", Value: "DMServerStaging",
}},
},
},
Vars: []string{
tape.ServiceAccountVar,
},
BugComponent: "b:1253671",
})
}
func SAMLEnrollment(ctx context.Context, s *testing.State) {
param := s.Param().(gaiaenrollment.TestParams)
dmServerURL := param.DMServer
poolID := param.PoolID
// Shorten deadline to leave time for cleanup.
cleanupCtx := ctx
ctx, cancel := ctxutil.Shorten(ctx, 3*time.Minute)
defer cancel()
defer func(ctx context.Context) {
if err := policyutil.EnsureTPMAndSystemStateAreReset(ctx, s.DUT(), s.RPCHint()); err != nil {
s.Error("Failed to reset TPM after test: ", err)
}
}(cleanupCtx)
if err := policyutil.EnsureTPMAndSystemStateAreReset(ctx, s.DUT(), s.RPCHint()); err != nil {
s.Fatal("Failed to reset TPM: ", err)
}
cl, err := rpc.Dial(ctx, s.DUT(), s.RPCHint())
if err != nil {
s.Fatal("Failed to connect to the RPC service on the DUT: ", err)
}
defer cl.Close(cleanupCtx)
screenshotService := graphics.NewScreenshotServiceClient(cl.Conn)
captureScreenshotOnError := func(ctx context.Context, hasError func() bool) {
if !hasError() {
return
}
screenshotService.CaptureScreenshot(ctx, &graphics.CaptureScreenshotRequest{FilePrefix: "enrollmentError"})
}
defer captureScreenshotOnError(cleanupCtx, s.HasError)
policyClient := pspb.NewPolicyServiceClient(cl.Conn)
tapeClient, err := tape.NewClient(ctx, []byte(s.RequiredVar(tape.ServiceAccountVar)))
if err != nil {
s.Fatal("Failed to create tape client: ", err)
}
timeout := int32(samlEnrollmentTimeout.Seconds())
// Create an account manager and lease a test account for the duration of the test.
accManager, acc, err := tape.NewOwnedTestAccountManagerFromClient(ctx, tapeClient, false /*lock*/, tape.WithTimeout(timeout), tape.WithPoolID(poolID))
if err != nil {
s.Fatal("Failed to create an account manager and lease an account: ", err)
}
defer accManager.CleanUp(cleanupCtx)
// Deprovision the DUT at the end of the test. As devices might get
// provisioned even when the enrollment fails we need to defer the
// deprovisioning before enrolling.
defer func(ctx context.Context) {
if err := tapeClient.DeprovisionHelper(cleanupCtx, cl, acc.OrgUnitPath); err != nil {
s.Fatal("Failed to deprovision device: ", err)
}
}(cleanupCtx)
if _, err := policyClient.SAMLTestIdPEnrollUsingChrome(ctx, &pspb.SAMLTestIdPEnrollUsingChromeRequest{
Username: acc.Username,
Password: acc.Password,
DmserverURL: dmServerURL,
}); err != nil {
s.Fatal("Failed to enroll using chrome: ", err)
}
defer policyClient.StopChrome(ctx, &empty.Empty{})
}