-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List Available Fleet Managed Apps #22059
Merged
dantecatalfamo
merged 20 commits into
feat-fleet-app-library
from
fleet-managed-apps-list-apps
Sep 19, 2024
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
eb34723
Initial outline of list fma
dantecatalfamo 9384161
Typos
dantecatalfamo 1d1df74
Change join format, add pagination, proper service method (no authz)
dantecatalfamo b72f6d7
Typos and interface mismatches
dantecatalfamo a868326
Generate mock
dantecatalfamo 55c2183
Merge branch 'feat-fleet-app-library' into fleet-managed-apps-list-apps
dantecatalfamo 3c3939b
Move functions to existing files, match platform on join as well
dantecatalfamo e2ee658
Updating tests, change function signature
dantecatalfamo 6519095
Merge branch 'feat-fleet-app-library' into fleet-managed-apps-list-apps
dantecatalfamo 17040bd
Query works, tests work
dantecatalfamo 8979533
Add changes/
dantecatalfamo 7a53c12
Feet maintained app id is an integer not a string
dantecatalfamo 3f0bcaf
Missing err check
dantecatalfamo 14e7690
Change fleet managed to Fleet-maintained
dantecatalfamo d2669a6
Use existing type for maintained apps list, add authz check
dantecatalfamo b510c95
Merge branch 'feat-fleet-app-library' into fleet-managed-apps-list-apps
dantecatalfamo a516667
Use returned value from upsert now in tests
dantecatalfamo b81b242
Add integration test
dantecatalfamo b8264f2
Fix other broken test
dantecatalfamo bf4cd3d
Move endpoint in handler
dantecatalfamo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- Add API endpoint to list team available Fleet-maintained apps |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
|
||
"github.com/fleetdm/fleet/v4/server/fleet" | ||
"github.com/fleetdm/fleet/v4/server/mdm/maintainedapps" | ||
"github.com/fleetdm/fleet/v4/server/test" | ||
"github.com/go-kit/kit/log" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/require" | ||
|
@@ -21,6 +22,7 @@ func TestMaintainedApps(t *testing.T) { | |
}{ | ||
{"UpsertMaintainedApps", testUpsertMaintainedApps}, | ||
{"IngestWithBrew", testIngestWithBrew}, | ||
{"ListAvailableApps", testListAvailableApps}, | ||
{"GetMaintainedAppByID", testGetMaintainedAppByID}, | ||
} | ||
|
||
|
@@ -35,8 +37,8 @@ func TestMaintainedApps(t *testing.T) { | |
func testUpsertMaintainedApps(t *testing.T, ds *Datastore) { | ||
ctx := context.Background() | ||
|
||
listSavedApps := func() []*fleet.MaintainedApp { | ||
var apps []*fleet.MaintainedApp | ||
listSavedApps := func() []fleet.MaintainedApp { | ||
var apps []fleet.MaintainedApp | ||
ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { | ||
return sqlx.SelectContext(ctx, q, &apps, "SELECT name, version, platform FROM fleet_library_apps ORDER BY token") | ||
}) | ||
|
@@ -61,9 +63,9 @@ func testUpsertMaintainedApps(t *testing.T, ds *Datastore) { | |
require.NoError(t, err) | ||
|
||
// change the expected app data for figma | ||
for _, app := range expectedApps { | ||
if app.Name == "Figma" { | ||
app.Version = "999.9.9" | ||
for idx := range expectedApps { | ||
if expectedApps[idx].Name == "Figma" { | ||
expectedApps[idx].Version = "999.9.9" | ||
break | ||
} | ||
} | ||
|
@@ -87,6 +89,238 @@ func testIngestWithBrew(t *testing.T, ds *Datastore) { | |
require.ElementsMatch(t, expectedTokens, actualTokens) | ||
} | ||
|
||
func testListAvailableApps(t *testing.T, ds *Datastore) { | ||
ctx := context.Background() | ||
|
||
user := test.NewUser(t, ds, "Zaphod Beeblebrox", "zaphod@example.com", true) | ||
team1, err := ds.NewTeam(ctx, &fleet.Team{Name: "Team 1"}) | ||
require.NoError(t, err) | ||
team2, err := ds.NewTeam(ctx, &fleet.Team{Name: "Team 2"}) | ||
require.NoError(t, err) | ||
|
||
maintained1, err := ds.UpsertMaintainedApp(ctx, &fleet.MaintainedApp{ | ||
Name: "Maintained1", | ||
Token: "maintained1", | ||
Version: "1.0.0", | ||
Platform: fleet.MacOSPlatform, | ||
InstallerURL: "http://example.com/main1", | ||
SHA256: "DEADBEEF", | ||
BundleIdentifier: "fleet.maintained1", | ||
InstallScript: "echo installed", | ||
UninstallScript: "echo uninstalled", | ||
}) | ||
require.NoError(t, err) | ||
maintained2, err := ds.UpsertMaintainedApp(ctx, &fleet.MaintainedApp{ | ||
Name: "Maintained2", | ||
Token: "maintained2", | ||
Version: "1.0.0", | ||
Platform: fleet.MacOSPlatform, | ||
InstallerURL: "http://example.com/main1", | ||
SHA256: "DEADBEEF", | ||
BundleIdentifier: "fleet.maintained2", | ||
InstallScript: "echo installed", | ||
UninstallScript: "echo uninstalled", | ||
}) | ||
require.NoError(t, err) | ||
maintained3, err := ds.UpsertMaintainedApp(ctx, &fleet.MaintainedApp{ | ||
Name: "Maintained3", | ||
Token: "maintained3", | ||
Version: "1.0.0", | ||
Platform: fleet.MacOSPlatform, | ||
InstallerURL: "http://example.com/main1", | ||
SHA256: "DEADBEEF", | ||
BundleIdentifier: "fleet.maintained3", | ||
InstallScript: "echo installed", | ||
UninstallScript: "echo uninstalled", | ||
}) | ||
require.NoError(t, err) | ||
|
||
expectedApps := []fleet.MaintainedApp{ | ||
{ | ||
ID: maintained1.ID, | ||
Name: maintained1.Name, | ||
Version: maintained1.Version, | ||
Platform: maintained1.Platform, | ||
}, | ||
{ | ||
ID: maintained2.ID, | ||
Name: maintained2.Name, | ||
Version: maintained2.Version, | ||
Platform: maintained2.Platform, | ||
}, | ||
{ | ||
ID: maintained3.ID, | ||
Name: maintained3.Name, | ||
Version: maintained3.Version, | ||
Platform: maintained3.Platform, | ||
}, | ||
} | ||
|
||
// Testing pagination | ||
apps, meta, err := ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 3) | ||
require.Equal(t, expectedApps, apps) | ||
require.False(t, meta.HasNextResults) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{PerPage: 1, IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 1) | ||
require.Equal(t, expectedApps[:1], apps) | ||
require.True(t, meta.HasNextResults) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{PerPage: 1, Page: 1, IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 1) | ||
require.Equal(t, expectedApps[1:2], apps) | ||
require.True(t, meta.HasNextResults) | ||
require.True(t, meta.HasPreviousResults) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{PerPage: 1, Page: 2, IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 1) | ||
require.Equal(t, expectedApps[2:3], apps) | ||
require.False(t, meta.HasNextResults) | ||
require.True(t, meta.HasPreviousResults) | ||
|
||
// | ||
// Test excluding results for existing apps (installers) | ||
|
||
/// Irrelevant package | ||
_, err = ds.MatchOrCreateSoftwareInstaller(ctx, &fleet.UploadSoftwareInstallerPayload{ | ||
Title: "Irrelevant Software", | ||
TeamID: &team1.ID, | ||
InstallScript: "nothing", | ||
Filename: "foo.pkg", | ||
UserID: user.ID, | ||
Platform: string(fleet.MacOSPlatform), | ||
BundleIdentifier: "irrelevant_1", | ||
}) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 3) | ||
require.Equal(t, expectedApps, apps) | ||
|
||
/// Correct package on a different team | ||
_, err = ds.MatchOrCreateSoftwareInstaller(ctx, &fleet.UploadSoftwareInstallerPayload{ | ||
Title: "Maintained1", | ||
TeamID: &team2.ID, | ||
InstallScript: "nothing", | ||
Filename: "foo.pkg", | ||
UserID: user.ID, | ||
Platform: string(fleet.MacOSPlatform), | ||
BundleIdentifier: "fleet.maintained1", | ||
}) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 3) | ||
require.Equal(t, expectedApps, apps) | ||
|
||
/// Correct package on the right team with the wrong platform | ||
_, err = ds.MatchOrCreateSoftwareInstaller(ctx, &fleet.UploadSoftwareInstallerPayload{ | ||
Title: "Maintained1", | ||
TeamID: &team1.ID, | ||
InstallScript: "nothing", | ||
Filename: "foo.pkg", | ||
UserID: user.ID, | ||
Platform: string(fleet.IOSPlatform), | ||
BundleIdentifier: "fleet.maintained1", | ||
}) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 3) | ||
require.Equal(t, expectedApps, apps) | ||
|
||
/// Correct team and platform | ||
ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error { | ||
_, err := q.ExecContext(ctx, "UPDATE software_installers SET platform = ? WHERE platform = ?", fleet.MacOSPlatform, fleet.IOSPlatform) | ||
return err | ||
}) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 2) | ||
require.Equal(t, expectedApps[1:], apps) | ||
|
||
// | ||
// Test excluding results for existing apps (VPP) | ||
|
||
test.CreateInsertGlobalVPPToken(t, ds) | ||
|
||
// irrelevant vpp app | ||
vppIrrelevant := &fleet.VPPApp{ | ||
Name: "irrelevant_app", | ||
VPPAppTeam: fleet.VPPAppTeam{ | ||
VPPAppID: fleet.VPPAppID{ | ||
AdamID: "1", | ||
Platform: fleet.MacOSPlatform, | ||
}, | ||
}, | ||
BundleIdentifier: "irrelevant_2", | ||
} | ||
_, err = ds.InsertVPPAppWithTeam(ctx, vppIrrelevant, &team1.ID) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 2) | ||
require.Equal(t, expectedApps[1:], apps) | ||
|
||
// right vpp app, wrong team | ||
vppMaintained2 := &fleet.VPPApp{ | ||
Name: "Maintained 2", | ||
VPPAppTeam: fleet.VPPAppTeam{ | ||
VPPAppID: fleet.VPPAppID{ | ||
AdamID: "2", | ||
Platform: fleet.MacOSPlatform, | ||
}, | ||
}, | ||
BundleIdentifier: "fleet.maintained2", | ||
} | ||
_, err = ds.InsertVPPAppWithTeam(ctx, vppMaintained2, &team2.ID) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 2) | ||
require.Equal(t, expectedApps[1:], apps) | ||
|
||
// right vpp app, right team | ||
_, err = ds.InsertVPPAppWithTeam(ctx, vppMaintained2, &team1.ID) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 1) | ||
require.Equal(t, expectedApps[2:], apps) | ||
|
||
// right app, right team, wrong platform | ||
vppMaintained3 := &fleet.VPPApp{ | ||
Name: "Maintained 3", | ||
VPPAppTeam: fleet.VPPAppTeam{ | ||
VPPAppID: fleet.VPPAppID{ | ||
AdamID: "3", | ||
Platform: fleet.IOSPlatform, | ||
}, | ||
}, | ||
BundleIdentifier: "fleet.maintained3", | ||
} | ||
|
||
_, err = ds.InsertVPPAppWithTeam(ctx, vppMaintained3, &team1.ID) | ||
require.NoError(t, err) | ||
|
||
apps, meta, err = ds.ListAvailableFleetMaintainedApps(ctx, team1.ID, fleet.ListOptions{IncludeMetadata: true}) | ||
require.NoError(t, err) | ||
require.Len(t, apps, 1) | ||
require.Equal(t, expectedApps[2:], apps) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice tests! |
||
|
||
func testGetMaintainedAppByID(t *testing.T, ds *Datastore) { | ||
ctx := context.Background() | ||
|
||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! so this was better than the subquery + where?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually never ended up testing the performance, although I assume this solution will work well.