Skip to content

Commit

Permalink
feat: addition of applens client and detector endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Jones committed Oct 18, 2022
1 parent f870b13 commit 802f034
Show file tree
Hide file tree
Showing 30 changed files with 1,956 additions and 37 deletions.
10 changes: 10 additions & 0 deletions pkg/api/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ var OperationOpenShiftClusterListAdminCredentials = Operation{
Origin: "user,system",
}

var OperationOpenShiftClusterGetDetectors = Operation{
Name: "Microsoft.RedHatOpenShift/openShiftClusters/detectors/read",
Display: Display{
Provider: "Azure Red Hat OpenShift",
Resource: "openShiftClusters",
Operation: "Get OpenShift Cluster Detector",
},
Origin: "user,system",
}

var OperationListInstallVersions = Operation{
Name: "Microsoft.RedHatOpenShift/locations/listInstallVersions/read",
Display: Display{
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v20191231preview/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
api.OperationOpenShiftClusterWrite,
api.OperationOpenShiftClusterDelete,
api.OperationOpenShiftClusterListCredentials,
api.OperationOpenShiftClusterGetDetectors,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v20200430/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
api.OperationOpenShiftClusterWrite,
api.OperationOpenShiftClusterDelete,
api.OperationOpenShiftClusterListCredentials,
api.OperationOpenShiftClusterGetDetectors,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v20210901preview/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
api.OperationOpenShiftClusterDelete,
api.OperationOpenShiftClusterListCredentials,
api.OperationOpenShiftClusterListAdminCredentials,
api.OperationOpenShiftClusterGetDetectors,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v20220401/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
api.OperationOpenShiftClusterDelete,
api.OperationOpenShiftClusterListCredentials,
api.OperationOpenShiftClusterListAdminCredentials,
api.OperationOpenShiftClusterGetDetectors,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/api/v20220904/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
api.OperationSyncIdentityProvidersRead,
api.OperationSyncIdentityProvidersWrite,
api.OperationSyncIdentityProvidersDelete,
api.OperationOpenShiftClusterGetDetectors,
},
},
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/env/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -98,3 +99,17 @@ func (d *dev) FPAuthorizer(tenantID, resource string) (refreshable.Authorizer, e

return refreshable.NewAuthorizer(sp), nil
}

func (d *dev) FPNewClientCertificateCredential(tenantID string) (*azidentity.ClientCertificateCredential, error) {
fpPrivateKey, fpCertificates := d.fpCertificateRefresher.GetCertificates()

credential, err := azidentity.NewClientCertificateCredential(tenantID, d.fpClientID, fpCertificates, fpPrivateKey, &azidentity.ClientCertificateCredentialOptions{
AuthorityHost: d.Environment().AuthorityHost,
})

if err != nil {
return nil, err
}

return credential, nil
}
2 changes: 2 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
mgmtcompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -81,6 +82,7 @@ type Interface interface {
Domain() string
FeatureIsSet(Feature) bool
FPAuthorizer(string, string) (refreshable.Authorizer, error)
FPNewClientCertificateCredential(string) (*azidentity.ClientCertificateCredential, error)
FPClientID() string
Listen() (net.Listener, error)
GatewayDomains() []string
Expand Down
15 changes: 15 additions & 0 deletions pkg/env/prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
mgmtcompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
Expand Down Expand Up @@ -370,3 +371,17 @@ func (p *prod) VMSku(vmSize string) (*mgmtcompute.ResourceSku, error) {
func (p *prod) LiveConfig() liveconfig.Manager {
return p.liveConfig
}

func (p *prod) FPNewClientCertificateCredential(tenantID string) (*azidentity.ClientCertificateCredential, error) {
fpPrivateKey, fpCertificates := p.fpCertificateRefresher.GetCertificates()

credential, err := azidentity.NewClientCertificateCredential(tenantID, p.fpClientID, fpCertificates, fpPrivateKey, &azidentity.ClientCertificateCredentialOptions{
AuthorityHost: p.Environment().AuthorityHost,
})

if err != nil {
return nil, err
}

return credential, nil
}
28 changes: 28 additions & 0 deletions pkg/frontend/adminactions/azureactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package adminactions

import (
"context"
"encoding/json"
"fmt"
"net/http"

Expand All @@ -13,6 +14,7 @@ import (

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/env"
"github.com/Azure/ARO-RP/pkg/util/azureclient/applens"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/compute"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/features"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/network"
Expand All @@ -30,6 +32,8 @@ type AzureActions interface {
VMSizeList(ctx context.Context) ([]mgmtcompute.ResourceSku, error)
VMResize(ctx context.Context, vmName string, vmSize string) error
VMSerialConsole(ctx context.Context, w http.ResponseWriter, log *logrus.Entry, vmName string) error
AppLensGetDetector(ctx context.Context, detectorId string) ([]byte, error)
AppLensListDetectors(ctx context.Context) ([]byte, error)
}

type azureActions struct {
Expand All @@ -45,6 +49,7 @@ type azureActions struct {
routeTables network.RouteTablesClient
storageAccounts storage.AccountsClient
networkInterfaces network.InterfacesClient
appLens applens.AppLensClient
}

// NewAzureActions returns an azureActions
Expand All @@ -56,6 +61,11 @@ func NewAzureActions(log *logrus.Entry, env env.Interface, oc *api.OpenShiftClus
return nil, err
}

fpClientCertCred, err := env.FPNewClientCertificateCredential(subscriptionDoc.Subscription.Properties.TenantID)
if err != nil {
return nil, err
}

return &azureActions{
log: log,
env: env,
Expand All @@ -69,6 +79,7 @@ func NewAzureActions(log *logrus.Entry, env env.Interface, oc *api.OpenShiftClus
routeTables: network.NewRouteTablesClient(env.Environment(), subscriptionDoc.ID, fpAuth),
storageAccounts: storage.NewAccountsClient(env.Environment(), subscriptionDoc.ID, fpAuth),
networkInterfaces: network.NewInterfacesClient(env.Environment(), subscriptionDoc.ID, fpAuth),
appLens: applens.NewAppLensClient(env.Environment(), fpClientCertCred),
}, nil
}

Expand Down Expand Up @@ -110,3 +121,20 @@ func (a *azureActions) VMResize(ctx context.Context, vmName string, size string)
vm.HardwareProfile.VMSize = mgmtcompute.VirtualMachineSizeTypes(size)
return a.virtualMachines.CreateOrUpdateAndWait(ctx, clusterRGName, vmName, vm)
}

func (a *azureActions) AppLensGetDetector(ctx context.Context, detectorId string) ([]byte, error) {
resp, err := a.appLens.GetDetector(ctx, &applens.GetDetectorOptions{ResourceID: a.oc.ID, DetectorID: detectorId})

if err != nil {
return nil, err
}
return json.Marshal(resp.Body)
}

func (a *azureActions) AppLensListDetectors(ctx context.Context) ([]byte, error) {
resp, err := a.appLens.ListDetectors(ctx, &applens.ListDetectorsOptions{ResourceID: a.oc.ID})
if err != nil {
return nil, err
}
return json.Marshal(resp.Body)
}
12 changes: 12 additions & 0 deletions pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ func (f *frontend) authenticatedRoutes(r *mux.Router) {

s.Methods(http.MethodGet).HandlerFunc(f.listInstallVersions).Name("listInstallVersions")

s = r.
Path("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}/detectors").
Subrouter()

s.Methods(http.MethodGet).HandlerFunc(f.listAppLensDetectors).Name("listAppLensDetectors")

s = r.
Path("/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}/detectors/{detectorId}").
Subrouter()

s.Methods(http.MethodGet).HandlerFunc(f.getAppLensDetector).Name("getAppLensDetector")

// Admin actions
s = r.
Path("/admin/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}/kubernetesobjects").
Expand Down
83 changes: 83 additions & 0 deletions pkg/frontend/openshiftcluster_applensdetectors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package frontend

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"
"net/http"
"path/filepath"
"strings"

"github.com/gorilla/mux"
"github.com/sirupsen/logrus"

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/adminactions"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)

func (f *frontend) listAppLensDetectors(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)

b, err := f._listAppLensDetectors(ctx, r, log)

reply(log, w, nil, b, err)
}

func (f *frontend) _listAppLensDetectors(ctx context.Context, r *http.Request, log *logrus.Entry) ([]byte, error) {
a, err := f._createAzureActionsFactory(ctx, r, log)
if err != nil {
return nil, err
}

return a.AppLensListDetectors(ctx)
}

func (f *frontend) getAppLensDetector(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)

b, err := f._appLensDetectors(ctx, r, log)

reply(log, w, nil, b, err)
}

func (f *frontend) _appLensDetectors(ctx context.Context, r *http.Request, log *logrus.Entry) ([]byte, error) {
a, err := f._createAzureActionsFactory(ctx, r, log)
if err != nil {
return nil, err
}

vars := mux.Vars(r)
return a.AppLensGetDetector(ctx, vars["detectorId"])
}

func (f *frontend) _createAzureActionsFactory(ctx context.Context, r *http.Request, log *logrus.Entry) (adminactions.AzureActions, error) {
vars := mux.Vars(r)

resourceID := strings.TrimSuffix(r.URL.Path, "/detectors")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", vars["resourceType"], vars["resourceName"], vars["resourceGroupName"])
case err != nil:
return nil, err
}

subscriptionDoc, err := f.getSubscriptionDocument(ctx, doc.Key)
if err != nil {
return nil, err
}

a, err := f.azureActionsFactory(log, f.env, doc.OpenShiftCluster, subscriptionDoc)
if err != nil {
return nil, err
}

return a, nil
}
Loading

0 comments on commit 802f034

Please sign in to comment.