Skip to content

Commit

Permalink
Allow multi-platform and "macos" filtering on software titles, includ…
Browse files Browse the repository at this point in the history
…e associated policies for software titles with VPP apps

Next up:

* Fail when deleting VPP apps with attached policies
* Automated tests
  • Loading branch information
iansltx committed Jan 5, 2025
1 parent af1fedf commit 9b6e710
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
11 changes: 6 additions & 5 deletions server/datastore/mysql/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,19 +1751,20 @@ func (ds *Datastore) getPoliciesBySoftwareTitleIDs(
SELECT
p.id AS id,
p.name AS name,
st.id AS software_title_id
COALESCE(si.title_id, va.title_id) AS software_title_id
FROM policies p
JOIN software_installers si ON p.software_installer_id = si.id
JOIN software_titles st ON si.title_id = st.id
WHERE st.id IN (?) AND p.team_id = ?
LEFT JOIN software_installers si ON p.software_installer_id = si.id
LEFT JOIN policy_vpp_automations pva ON p.id = pva.policy_id
LEFT JOIN vpp_apps va ON va.adam_id = pva.adam_id AND va.platform = pva.platform
WHERE (va.title_id IN (?) OR si.title_id IN (?)) AND p.team_id = ?
`

var tmID uint
if teamID != nil {
tmID = *teamID
}

query, args, err := sqlx.In(query, softwareTitleIDs, tmID)
query, args, err := sqlx.In(query, softwareTitleIDs, softwareTitleIDs, tmID)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "build select get policies by software id query")
}
Expand Down
22 changes: 18 additions & 4 deletions server/datastore/mysql/software_titles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -142,7 +143,7 @@ func (ds *Datastore) ListSoftwareTitles(

// grab all the IDs to find matching versions below
titleIDs := make([]uint, len(softwareList))
// build an index to quickly access a title by it's ID
// build an index to quickly access a title by its ID
titleIndex := make(map[uint]int, len(softwareList))
for i, title := range softwareList {
// promote the package name and version to the proper destination fields
Expand Down Expand Up @@ -188,7 +189,11 @@ func (ds *Datastore) ListSoftwareTitles(

for _, p := range policies {
if i, ok := titleIndex[p.TitleID]; ok {
softwareList[i].SoftwarePackage.AutomaticInstallPolicies = append(softwareList[i].SoftwarePackage.AutomaticInstallPolicies, p)
if softwareList[i].AppStoreApp != nil {
softwareList[i].AppStoreApp.AutomaticInstallPolicies = append(softwareList[i].SoftwarePackage.AutomaticInstallPolicies, p)

Check failure on line 193 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

appendAssign: append result not assigned to the same slice (gocritic)

Check failure on line 193 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

appendAssign: append result not assigned to the same slice (gocritic)
} else {
softwareList[i].SoftwarePackage.AutomaticInstallPolicies = append(softwareList[i].SoftwarePackage.AutomaticInstallPolicies, p)
}
}
}

Expand Down Expand Up @@ -379,8 +384,17 @@ GROUP BY st.id, package_self_service, package_name, package_version, package_url
}

if opt.Platform != "" {
additionalWhere += ` AND ( si.platform = ? OR vap.platform = ? )`
args = append(args, opt.Platform, opt.Platform)
platforms := strings.Split(strings.Replace(opt.Platform, "macos", "darwin", -1), ",")

Check failure on line 387 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(opt.Platform, "macos", "darwin", -1)` (gocritic)

Check failure on line 387 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

wrapperFunc: use strings.ReplaceAll method in `strings.Replace(opt.Platform, "macos", "darwin", -1)` (gocritic)
platformPlaceholders := strings.TrimSuffix(strings.Repeat("?,", len(platforms)), ",")

additionalWhere += fmt.Sprintf(` AND (si.platform IN (%s) OR vap.platform IN (%s))`, platformPlaceholders, platformPlaceholders)
slices.Grow(args, len(platformPlaceholders)*2)

Check failure on line 391 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

unusedresult: result of slices.Grow call not used (govet)

Check failure on line 391 in server/datastore/mysql/software_titles.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

unusedresult: result of slices.Grow call not used (govet)
for _, platform := range platforms { // for software installers
args = append(args, platform)
}
for _, platform := range platforms { // for VPP apps; could micro-optimize later by dropping non-Apple platforms
args = append(args, platform)
}
}

// default to "a software installer or VPP app exists", and see next condition.
Expand Down

0 comments on commit 9b6e710

Please sign in to comment.