Skip to content

Commit

Permalink
Merge branch 'master' into OTT-48
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-viral-vala committed Jan 18, 2021
1 parent a17348d commit c5e9530
Show file tree
Hide file tree
Showing 961 changed files with 53,691 additions and 11,409 deletions.
18 changes: 18 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# From https://github.com/microsoft/vscode-dev-containers/blob/master/containers/go/.devcontainer/Dockerfile
ARG VARIANT=1
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}

# [Optional] Install a version of Node.js using nvm for front end dev
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends vim

# [Optional] Uncomment the next line to use go get to install anything else you need
# RUN go get -x <your-dependency-or-tool>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
45 changes: 45 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/go
{
"name": "Go",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.15, 1.14
"VARIANT": "1.14",
// Options
"INSTALL_NODE": "false",
"NODE_VERSION": "lts/*",
}
},
"containerEnv": {
"GOPRIVATE": "${localEnv:GOPRIVATE}",
},
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"go.useGoProxyToCheckForToolUpdates": false,
"go.gopath": "/go",
//"go.toolsGopath": "/tmp/go",
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"ms-azuretools.vscode-docker",
"redhat.vscode-xml",
"redhat.vscode-yaml",
"eamodio.gitlens",
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8000,8001,6060],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "mkdir ~/.ssh; ssh-keyscan github.com > ~/.ssh/known_hosts",

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
28 changes: 28 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
push:
branches:
- master
pull_request:
release:
types:
- created
name: Validate
jobs:
Go:
strategy:
matrix:
go-version: [1.13.x, 1.14.x, 1.15.x]
os: [ubuntu-18.04]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Validate
run: |
./validate.sh --nofmt --cov --race 10
env:
GO111MODULE: "on"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ _obj
_test
.cover/
.idea/
.vscode/

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down Expand Up @@ -46,6 +45,7 @@ analytics/filesystem/testFiles/
# static/version.txt

.idea/
.vscode/

# autogenerated mac file

Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ It is managed by [Prebid.org](http://prebid.org/overview/what-is-prebid-org.html
and upholds the principles from the [Prebid Code of Conduct](http://prebid.org/wrapper_code_of_conduct.html).

This project does not support the same set of Bidders as Prebid.js, although there is overlap.
The current set can be found in the [adapters](./adapters) package. If you don't see the one you want, feel free to [contribute it](docs/developers/add-new-bidder.md).
The current set can be found in the [adapters](./adapters) package. If you don't see the one you want, feel free to [contribute it](https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html).

For more information, see:

- [What is Prebid?](http://prebid.org/overview/intro.html)
- [Getting started with Prebid Server](http://prebid.org/dev-docs/get-started-with-prebid-server.html)
- [Current Bidders](http://prebid.org/dev-docs/prebid-server-bidders.html)
- [What is Prebid?](https://prebid.org/overview/intro.html)
- [Prebid Server Overview](https://docs.prebid.org/prebid-server/overview/prebid-server-overview.html)
- [Current Bidders](http://prebid.org/dev-docs/pbs-bidders.html)

## Installation

Expand Down Expand Up @@ -45,15 +45,18 @@ go build .
```

Load the landing page in your browser at `http://localhost:8000/`.
For the full API reference, see [docs/endpoints](docs/endpoints)
For the full API reference, see [the endpoint documentation](https://docs.prebid.org/prebid-server/endpoints/pbs-endpoint-overview.html)


## Contributing

Want to [add an adapter](docs/developers/add-new-bidder.md)? Found a bug? Great!
This project is in its infancy, and many things can be improved.

Want to [add an adapter](https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html)? Found a bug? Great!

Report bugs, request features, and suggest improvements [on Github](https://github.com/PubMatic-OpenWrap/prebid-server/issues).

Or better yet, [open a pull request](https://github.com/PubMatic-OpenWrap/prebid-server/compare) with the changes you'd like to see.

## IDE Setup for PBS-Go development

The quickest way to start developing PBS-Go in a reproducible environment isolated from your host OS
is by using this [VScode Remote Container Setup](devcontainer.md)
69 changes: 69 additions & 0 deletions account/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package account

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

"github.com/PubMatic-OpenWrap/prebid-server/config"
"github.com/PubMatic-OpenWrap/prebid-server/errortypes"
"github.com/PubMatic-OpenWrap/prebid-server/pbsmetrics"
"github.com/PubMatic-OpenWrap/prebid-server/stored_requests"
jsonpatch "github.com/evanphx/json-patch"
)

// GetAccount looks up the config.Account object referenced by the given accountID, with access rules applied
func GetAccount(ctx context.Context, cfg *config.Configuration, fetcher stored_requests.AccountFetcher, accountID string) (account *config.Account, errs []error) {
// Check BlacklistedAcctMap until we have deprecated it
if _, found := cfg.BlacklistedAcctMap[accountID]; found {
return nil, []error{&errortypes.BlacklistedAcct{
Message: fmt.Sprintf("Prebid-server has disabled Account ID: %s, please reach out to the prebid server host.", accountID),
}}
}
if cfg.AccountRequired && accountID == pbsmetrics.PublisherUnknown {
return nil, []error{&errortypes.AcctRequired{
Message: fmt.Sprintf("Prebid-server has been configured to discard requests without a valid Account ID. Please reach out to the prebid server host."),
}}
}
if accountJSON, accErrs := fetcher.FetchAccount(ctx, accountID); len(accErrs) > 0 || accountJSON == nil {
// accountID does not reference a valid account
for _, e := range accErrs {
if _, ok := e.(stored_requests.NotFoundError); !ok {
errs = append(errs, e)
}
}
if cfg.AccountRequired && cfg.AccountDefaults.Disabled {
errs = append(errs, &errortypes.AcctRequired{
Message: fmt.Sprintf("Prebid-server could not verify the Account ID. Please reach out to the prebid server host."),
})
return nil, errs
}
// Make a copy of AccountDefaults instead of taking a reference,
// to preserve original accountID in case is needed to check NonStandardPublisherMap
pubAccount := cfg.AccountDefaults
pubAccount.ID = accountID
account = &pubAccount
} else {
// accountID resolved to a valid account, merge with AccountDefaults for a complete config
account = &config.Account{}
completeJSON, err := jsonpatch.MergePatch(cfg.AccountDefaultsJSON(), accountJSON)
if err == nil {
err = json.Unmarshal(completeJSON, account)
}
if err != nil {
errs = append(errs, err)
return nil, errs
}
// Fill in ID if needed, so it can be left out of account definition
if len(account.ID) == 0 {
account.ID = accountID
}
}
if account.Disabled {
errs = append(errs, &errortypes.BlacklistedAcct{
Message: fmt.Sprintf("Prebid-server has disabled Account ID: %s, please reach out to the prebid server host.", accountID),
})
return nil, errs
}
return account, nil
}
94 changes: 94 additions & 0 deletions account/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package account

import (
"context"
"encoding/json"
"fmt"
"testing"

"github.com/PubMatic-OpenWrap/prebid-server/config"
"github.com/PubMatic-OpenWrap/prebid-server/errortypes"
"github.com/PubMatic-OpenWrap/prebid-server/pbsmetrics"
"github.com/PubMatic-OpenWrap/prebid-server/stored_requests"
"github.com/stretchr/testify/assert"
)

var mockAccountData = map[string]json.RawMessage{
"valid_acct": json.RawMessage(`{"disabled":false}`),
"disabled_acct": json.RawMessage(`{"disabled":true}`),
}

type mockAccountFetcher struct {
}

func (af mockAccountFetcher) FetchAccount(ctx context.Context, accountID string) (json.RawMessage, []error) {
if account, ok := mockAccountData[accountID]; ok {
return account, nil
}
return nil, []error{stored_requests.NotFoundError{accountID, "Account"}}
}

func TestGetAccount(t *testing.T) {
unknown := pbsmetrics.PublisherUnknown
testCases := []struct {
accountID string
// account_required
required bool
// account_defaults.disabled
disabled bool
// expected error, or nil if account should be found
err error
}{
// Blacklisted account is always rejected even in permissive setup
{accountID: "bad_acct", required: false, disabled: false, err: &errortypes.BlacklistedAcct{}},

// empty pubID
{accountID: unknown, required: false, disabled: false, err: nil},
{accountID: unknown, required: true, disabled: false, err: &errortypes.AcctRequired{}},
{accountID: unknown, required: false, disabled: true, err: &errortypes.BlacklistedAcct{}},
{accountID: unknown, required: true, disabled: true, err: &errortypes.AcctRequired{}},

// pubID given but is not a valid host account (does not exist)
{accountID: "doesnt_exist_acct", required: false, disabled: false, err: nil},
{accountID: "doesnt_exist_acct", required: true, disabled: false, err: nil},
{accountID: "doesnt_exist_acct", required: false, disabled: true, err: &errortypes.BlacklistedAcct{}},
{accountID: "doesnt_exist_acct", required: true, disabled: true, err: &errortypes.AcctRequired{}},

// pubID given and matches a valid host account with Disabled: false
{accountID: "valid_acct", required: false, disabled: false, err: nil},
{accountID: "valid_acct", required: true, disabled: false, err: nil},
{accountID: "valid_acct", required: false, disabled: true, err: nil},
{accountID: "valid_acct", required: true, disabled: true, err: nil},

// pubID given and matches a host account explicitly disabled (Disabled: true on account json)
{accountID: "disabled_acct", required: false, disabled: false, err: &errortypes.BlacklistedAcct{}},
{accountID: "disabled_acct", required: true, disabled: false, err: &errortypes.BlacklistedAcct{}},
{accountID: "disabled_acct", required: false, disabled: true, err: &errortypes.BlacklistedAcct{}},
{accountID: "disabled_acct", required: true, disabled: true, err: &errortypes.BlacklistedAcct{}},
}

for _, test := range testCases {
description := fmt.Sprintf(`ID=%s/required=%t/disabled=%t`, test.accountID, test.required, test.disabled)
t.Run(description, func(t *testing.T) {
cfg := &config.Configuration{
BlacklistedAcctMap: map[string]bool{"bad_acct": true},
AccountRequired: test.required,
AccountDefaults: config.Account{Disabled: test.disabled},
}
fetcher := &mockAccountFetcher{}
assert.NoError(t, cfg.MarshalAccountDefaults())

account, errors := GetAccount(context.Background(), cfg, fetcher, test.accountID)

if test.err == nil {
assert.Empty(t, errors)
assert.Equal(t, test.accountID, account.ID, "account.ID must match requested ID")
assert.Equal(t, false, account.Disabled, "returned account must not be disabled")
} else {
assert.NotEmpty(t, errors, "expected errors but got success")
assert.Nil(t, account, "return account must be nil on error")
assert.IsType(t, test.err, errors[0], "error is of unexpected type")
}
})
}
}
Loading

0 comments on commit c5e9530

Please sign in to comment.