Skip to content

Commit

Permalink
✨ add ability to fetch ms365 application by id
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey committed Jan 15, 2025
1 parent bd6c42f commit b309aee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
71 changes: 56 additions & 15 deletions providers/ms365/resources/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,54 @@ func newMqlMicrosoftApplication(runtime *plugin.Runtime, app models.Applicationa
}

func initMicrosoftApplication(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
// we only look up the application if we have been supplied by its name and nothing else
raw, ok := args["name"]
if !ok || len(args) != 1 {
return args, nil, nil
var appId string

raw, ok := args["id"]
if ok && len(args) < 3 {
appId = raw.Value.(string)
}
if appId == "" {
// lookup by name
raw, ok := args["name"]
if ok && len(args) == 1 {
name := raw.Value.(string)
id, err := fetchApplicationIdByName(runtime, name)
if err != nil {
return nil, nil, err
}
appId = *id
}
}
name := raw.Value.(string)
if appId == "" {
return nil, nil, errors.New("need name or id to fetch application")
}

ctx := context.Background()

conn := runtime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
return nil, nil, err
}
// https://graph.microsoft.com/v1.0/applications/{application-id}
app, err := graphClient.Applications().ByApplicationId(appId).Get(ctx, &applications.ApplicationItemRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, nil, transformError(err)
}
mqlMsApp, err := newMqlMicrosoftApplication(runtime, app)
if err != nil {
return nil, nil, err
}

return nil, mqlMsApp, nil
}

func fetchApplicationIdByName(runtime *plugin.Runtime, name string) (*string, error) {
conn := runtime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
return nil, err
}

// https://graph.microsoft.com/v1.0/servicePrincipals?$count=true&$search="displayName:teams"&$select=id,displayName
filter := fmt.Sprintf("displayName eq '%s'", url.QueryEscape(name))
Expand All @@ -198,30 +234,35 @@ func initMicrosoftApplication(runtime *plugin.Runtime, args map[string]*llx.RawD
},
})
if err != nil {
return nil, nil, transformError(err)
return nil, transformError(err)
}

val := resp.GetValue()
if len(val) == 0 {
return nil, nil, errors.New("application not found")
return nil, errors.New("application not found")
}

applicationId := val[0].GetId()
if applicationId == nil {
return nil, nil, errors.New("application id not found")
return nil, errors.New("application id not found")
}
return applicationId, nil
}

// https://graph.microsoft.com/v1.0/applications/{application-id}
app, err := graphClient.Applications().ByApplicationId(*applicationId).Get(ctx, &applications.ApplicationItemRequestBuilderGetRequestConfiguration{})
func fetchApplicationById(runtime *plugin.Runtime, id string) (models.Applicationable, error) {
conn := runtime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
return nil, nil, transformError(err)
return nil, err
}
mqlMsApp, err := newMqlMicrosoftApplication(runtime, app)

ctx := context.Background()
// https://graph.microsoft.com/v1.0/applications/{application-id}
app, err := graphClient.Applications().ByApplicationId(id).Get(ctx, &applications.ApplicationItemRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, nil, err
return nil, transformError(err)
}

return nil, mqlMsApp, nil
return app, nil
}

// hasExpiredCredentials returns true if any of the credentials of the application are expired
Expand Down
2 changes: 1 addition & 1 deletion providers/ms365/resources/ms365.lr
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private microsoft.domaindnsrecord @defaults("id label") {

// Microsoft Entra ID application registration
microsoft.application @defaults("id displayName hasExpiredCredentials") {
init(name string)
init(name string, id string)
// Object ID
id string
// Application (client) ID
Expand Down

0 comments on commit b309aee

Please sign in to comment.