Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
discovery agent get all active flag is not working on webmethods
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed May 15, 2023
1 parent c8fab35 commit 266a521
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
working-directory: .
run: |
make build-discovery
# - name: Build traceability agent
# working-directory: .
# run: |
# make build-traceability
- name: Build traceability agent
working-directory: .
run: |
make build-trace
39 changes: 39 additions & 0 deletions .github/workflows/build-traceability-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Traceability Agent Docker

on:
push:
tags:
- "*"

jobs:
buildDiscoveryAgentDocker:
env:
GOFLAGS: "-mod=mod"
GOWORK: "off"
IMAGE_NAME: webmethods_traceability_agent
ORG_NAME: Axway
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
flavor: latest=false
images: ghcr.io/${{ env.ORG_NAME }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
file: build/traceability.Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
55 changes: 34 additions & 21 deletions build/traceability.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,57 @@
# golang:1.19.8-alpine3.17 linux/amd64
FROM docker.io/golang@sha256:841c160ed35923d96c95c52403c4e6db5decd9cbce034aa851e412ade5d4b74f as builder

ENV APP_HOME /build
ENV APP_HOME /go/src/github.com/Axway/agents-webmethods
ENV APP_USER axway
ENV AGENT=${APP_HOME}/cmd/traceability

RUN mkdir -p $APP_HOME /app

RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME

# Copy necessary files
COPY . .

RUN rm -rf bin

RUN make download
RUN make verify
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make build-trace-docker
RUN export time=`date +%Y%m%d%H%M%S` && \
export commit_id=`git rev-parse --short HEAD` && \
export version=`git tag -l --sort='version:refname' | grep -Eo '[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,3}$' | tail -1` && \
export sdk_version=`go list -m github.com/Axway/agent-sdk | awk '{print $2}' | awk -F'-' '{print substr($1, 2)}'` && \
export GOOS=linux && \
export CGO_ENABLED=0 && \
export GOARCH=amd64 && \
go build -tags static_all \
-ldflags="-X 'github.com/Axway/agent-sdk/pkg/cmd.BuildTime=${time}' \
-X 'github.com/Axway/agent-sdk/pkg/cmd.BuildVersion=${version}' \
-X 'github.com/Axway/agent-sdk/pkg/cmd.BuildCommitSha=${commit_id}' \
-X 'github.com/Axway/agent-sdk/pkg/cmd.SDKBuildVersion=${sdk_version}' \
-X 'github.com/Axway/agent-sdk/pkg/cmd.BuildAgentName=webMethodsTraceabilityAgent'" \
-a -o ${APP_HOME}/bin/webmethods_traceability_agent ${AGENT}/main.go

# Create non-root user
RUN addgroup $APP_USER && adduser --system $APP_USER --ingroup $APP_USER
RUN addgroup -g 2500 $APP_USER && adduser -u 2500 -D -G $APP_USER $APP_USER
RUN chown -R $APP_USER:$APP_USER ${APP_HOME}/bin/webmethods_traceability_agent

RUN mkdir /app/data && \
apk add ca-certificates && apk update && update-ca-certificates \
apk --no-cache add curl=7.69.1-r0 && \
chown -R $APP_USER /data && \
find / -perm /6000 -type f -exec chmod a-s {} \; || true

RUN chgrp -R 0 /app && chmod -R g=u /app && chown -R $APP_USER /app
USER $APP_USER

# alpine 3.17.3
FROM docker.io/alpine@sha256:b6ca290b6b4cdcca5b3db3ffa338ee0285c11744b4a6abaa9627746ee3291d8d
ENV APP_HOME /build

ENV APP_USER axway
ENV APP_HOME /go/src/github.com/Axway/agents-webmethods

# Copy binary, user, config file and certs from previous build step
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /app /app
COPY --from=builder $APP_HOME/build/webmethods_traceability_agent.yml /app/webmethods_traceability_agent.yml
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder $APP_HOME/build/webmethods_traceability_agent.yml /webmethods_traceability_agent.yml
COPY --from=builder ${APP_HOME}/bin/webmethods_traceability_agent /webmethods_traceability_agent

RUN mkdir /keys /data /events && \
chown -R axway /keys /data /events && \
apk --no-cache add openssl libssl libcrypto musl musl-utils libc6-compat busybox curl && \
find / -perm /6000 -type f -exec chmod a-s {} \; || true


USER $APP_USER
VOLUME ["/tmp"]
VOLUME ["/keys","/events", "/data"]
HEALTHCHECK --retries=1 CMD curl --fail http://localhost:${STATUS_PORT:-8989}/status || exit 1
ENTRYPOINT ["/app/traceability","--path.config", "/app"]
ENTRYPOINT ["/webmethods_traceability_agent"]
6 changes: 2 additions & 4 deletions cmd/traceability/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"fmt"
"os"

// Required Import to setup factory for traceability transport
_ "github.com/Axway/agent-sdk/pkg/traceability"

"github.com/Axway/agents-webmethods/pkg/cmd/traceability"
agentCmd "github.com/Axway/agents-webmethods/pkg/cmd/traceability"
)

func main() {
if err := traceability.RootCmd.Execute(); err != nil {
if err := agentCmd.RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/cmd/traceability/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package traceability

import (
"fmt"

corecmd "github.com/Axway/agent-sdk/pkg/cmd"
"github.com/Axway/agent-sdk/pkg/cmd/service"
corecfg "github.com/Axway/agent-sdk/pkg/config"
Expand All @@ -17,7 +15,6 @@ var RootCmd corecmd.AgentRootCmd
var beatCmd *libcmd.BeatsRootCmd

func init() {
fmt.Println("initanliing root")
name := "webmethods_traceability_agent"
settings := instance.Settings{
Name: name,
Expand All @@ -44,8 +41,6 @@ func init() {

// Callback that agent will call to process the execution
func run() error {
fmt.Println("initanliing root")

return beatCmd.Execute()
}

Expand Down
78 changes: 39 additions & 39 deletions pkg/discovery/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,56 +60,56 @@ func (d *discovery) Loop() {

// discoverAPIs Finds APIs from exchange
func (d *discovery) discoverAPIs() {
apis, err := d.client.ListAPIs()
apis, err := d.client.SearchAPIs()
log.Infof("%v", apis)
if err != nil {
log.Errorf("Unable to list Apis :%v", err)
return
}

for _, api := range apis {
go func(api webmethods.ListApiResponse) {
if api.ResponseStatus == "SUCCESS" {
apiResponse, err := d.client.GetApiDetails(api.WebmethodsApi.Id)
for _, api := range apis.WebmethodsApi {
go func(api webmethods.WebmethodsApi) {
apiResponse, err := d.client.GetApiDetails(api.Id)
if err != nil {
log.Errorf("Unable to get API Details : %v", err)
return
}

if !d.client.IsAllowedTags(apiResponse.Api.ApiDefinition.Tags) {
log.Infof("API not matched with filtered tags : %v, hence ignoring for discovery", err)
return
}

if apiResponse.Api.MaturityState == d.maturityState {
var specification []byte
if api.ApiType == "REST" {
specification, err = d.client.GetApiSpec(api.Id)
} else if api.ApiType == "SOAP" {
specification, err = d.client.GetWsdl(apiResponse.GatewayEndPoints[0])
}
if err != nil {
log.Errorf("Unable to get API Details : %v", err)
log.Errorf("Unable to read API specification : %v", err)
return
}

if !d.client.IsAllowedTags(apiResponse.Api.ApiDefinition.Tags) {
log.Infof("API matched with filtered tags : %v, hence ignoring for discovery", err)
return
amplifyApi := webmethods.AmplifyAPI{
ID: api.Id,
Name: api.ApiName,
Description: api.ApiDescription,
Version: api.ApiVersion,
// append endpoint url
Url: apiResponse.GatewayEndPoints[0],
Documentation: []byte(apiResponse.Api.ApiDefinition.Info.Description),
ApiSpec: specification,
ApiType: api.ApiType,
}

if apiResponse.Api.MaturityState == d.maturityState {
var specification []byte
if api.WebmethodsApi.ApiType == "REST" {
specification, err = d.client.GetApiSpec(api.WebmethodsApi.Id)
} else if api.WebmethodsApi.ApiType == "SOAP" {
specification, err = d.client.GetWsdl(apiResponse.GatewayEndPoints[0])
}
if err != nil {
log.Error("Unable to read API specification : ", err)
return
}
amplifyApi := webmethods.AmplifyAPI{
ID: api.WebmethodsApi.Id,
Name: api.WebmethodsApi.ApiName,
Description: api.WebmethodsApi.ApiDescription,
Version: api.WebmethodsApi.ApiVersion,
// append endpoint url
Url: apiResponse.GatewayEndPoints[0],
Documentation: []byte(apiResponse.Api.ApiDefinition.Info.Description),
ApiSpec: specification,
ApiType: api.WebmethodsApi.ApiType,
}
svcDetail := d.serviceHandler.ToServiceDetail(&amplifyApi)
if svcDetail != nil {
d.apiChan <- svcDetail
}
} else {
log.Infof("Ignoring API %s with MaturityState %s", api.WebmethodsApi.ApiName, apiResponse.Api.MaturityState)
svcDetail := d.serviceHandler.ToServiceDetail(&amplifyApi)
if svcDetail != nil {
d.apiChan <- svcDetail
}
} else {
log.Infof("Ignoring API %s with MaturityState %s", api.ApiName, apiResponse.Api.MaturityState)
}

}(api)
}
}
2 changes: 0 additions & 2 deletions pkg/traceability/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package traceability

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -19,7 +18,6 @@ import (
)

func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
fmt.Println("beater")
err := validateInput(cfg)
if err != nil {
return nil, localerrors.ErrInvalidInputConfig.FormatError(err.Error())
Expand Down
42 changes: 42 additions & 0 deletions pkg/webmethods/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Page struct {
type Client interface {
createAuthToken() string
ListAPIs() ([]ListApiResponse, error)
SearchAPIs() (*Apis, error)
GetApiDetails(id string) (*ApiResponse, error)
IsAllowedTags(tags []Tag) bool
GetApiSpec(id string) ([]byte, error)
Expand Down Expand Up @@ -142,6 +143,47 @@ func (c *WebMethodClient) ListAPIs() ([]ListApiResponse, error) {
return listApi.ListApiResponse, nil
}

func (c *WebMethodClient) SearchAPIs() (*Apis, error) {
//webmethodsApis := make([]WebmethodsApi, 0)
url := fmt.Sprintf("%s/rest/apigateway/search", c.url)
requestStr := `{
"types": [
"api"
],
"condition": "and",
"scope": [
{
"attributeName": "isActive",
"keyword": true
}
],
"sortByField": "apiName",
"sortOrder": "ASC"
}`
apis := &Apis{}
headers := map[string]string{
"Authorization": c.createAuthToken(),
"Content-Type": "application/json",
}

request := coreapi.Request{
Method: coreapi.POST,
URL: url,
Headers: headers,
Body: []byte(requestStr),
}
response, err := c.httpClient.Send(request)
if err != nil {
return nil, err
}

err = json.Unmarshal(response.Body, apis)
if err != nil {
return nil, err
}
return apis, nil
}

// ListAPIs lists webmethods APIM apis.
func (c *WebMethodClient) GetApiDetails(id string) (*ApiResponse, error) {
getApiDetails := &GetApiDetails{}
Expand Down
Loading

0 comments on commit 266a521

Please sign in to comment.