Exemplo n.º 1
0
 def setUp(self):
     super(QueryTests, self).setUp()
     keen._client = None
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     keen.read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]})
Exemplo n.º 2
0
 def setUp(self):
     super(CustomJSONEncoderTests, self).setUp()
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     self.client = KeenClient(project_id='5004ded1163d66114f000000',
                              write_key=scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}),
                              read_key=scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]}),
                              json_encoder=CustomEncoder)
Exemplo n.º 3
0
    def test_passing_invalid_custom_api_client(self, get):
        class CustomApiClient(object):
            api_version = "3.0"

            def __init__(self,
                         project_id,
                         write_key=None,
                         read_key=None,
                         base_url=None,
                         api_version=None,
                         **kwargs):
                super(CustomApiClient, self).__init__()
                self.project_id = project_id
                self.write_key = write_key
                self.read_key = read_key
                if base_url:
                    self.base_url = base_url
                if api_version:
                    self.api_version = api_version

        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        client = KeenClient(
            "5004ded1163d66114f000000",
            write_key=scoped_keys.encrypt(api_key,
                                          {"allowed_operations": ["write"]}),
            read_key=scoped_keys.encrypt(api_key,
                                         {"allowed_operations": ["read"]}),
            api_class=CustomApiClient,
            base_url="keen.base.url")

        # Should raise an error, we never added this method on our class
        # But it shows it is actually using our class
        self.assertRaises(TypeError, client.add_event)
Exemplo n.º 4
0
    def test_passing_invalid_custom_api_client(self, get):
        class CustomApiClient(object):
            api_version = "3.0"

            def __init__(self, project_id, write_key=None, read_key=None,
                         base_url=None, api_version=None, **kwargs):
                super(CustomApiClient, self).__init__()
                self.project_id = project_id
                self.write_key = write_key
                self.read_key = read_key
                if base_url:
                    self.base_url = base_url
                if api_version:
                    self.api_version = api_version

        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        client = KeenClient("5004ded1163d66114f000000",
            write_key=scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}),
            read_key=scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]}),
            api_class=CustomApiClient,
            base_url="keen.base.url")

        # Should raise an error, we never added this method on our class
        # But it shows it is actually using our class
        self.assertRaises(TypeError, client.add_event)
Exemplo n.º 5
0
 def setUp(self):
     super(QueryTests, self).setUp()
     keen._client = None
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     keen.read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]})
 def setUp(self):
     super(QueryTests, self).setUp()
     keen._client = None
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     keen.read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]})
     keen.add_event("query test", {"number": 5, "string": "foo"})
     keen.add_event("step2", {"number": 5, "string": "foo"})
Exemplo n.º 7
0
 def setUp(self):
     super(QueryTests, self).setUp()
     keen._client = None
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key,
                                          {"allowed_operations": ["write"]})
     keen.read_key = scoped_keys.encrypt(api_key,
                                         {"allowed_operations": ["read"]})
     keen.add_event("query test", {"number": 5, "string": "foo"})
     keen.add_event("step2", {"number": 5, "string": "foo"})
 def test_post_timeout_batch(self):
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     client = KeenClient(keen.project_id, write_key=keen.write_key, read_key=None,
                         post_timeout=0.0001)
     client.add_events({"python_test": [{"hello": "goodbye"}]})
    def test_generate_image_beacon(self):
        keen.project_id = "5004ded1163d66114f000000"
        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})

        event_collection = "python_test hello!?"
        event_data = {"a": "b"}
        data = self.base64_encode(json.dumps(event_data))

        # module level should work
        url = keen.generate_image_beacon(event_collection, event_data)
        expected = "https://api.keen.io/3.0/projects/{0}/events/{1}?api_key={2}&data={3}".format(
            keen.project_id, self.url_escape(event_collection), keen.write_key.decode(sys.getdefaultencoding()), data
        )
        self.assert_equal(expected, url)

        # so should instance level
        client = KeenClient(keen.project_id, write_key=keen.write_key, read_key=None)
        url = client.generate_image_beacon(event_collection, event_data)
        self.assert_equal(expected, url)

        # make sure URL works
        response = requests.get(url)
        self.assert_equal(200, response.status_code)
        self.assert_equal(b"GIF89a\x01\x00\x01\x00\x80\x01\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\x01\n\x00\x01\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02L\x01\x00;",
                          response.content)
Exemplo n.º 10
0
 def test_old_scoped_key_fails_decryption_on_bad_key(self):
     encrypted = scoped_keys.encrypt(self.old_api_key, self.options)
     try:
         scoped_keys.decrypt(self.old_bad_api_key, encrypted)
         self.fail("shouldn't get here")
     except ValueError as e:
         self.assert_not_equal(e, None)
Exemplo n.º 11
0
 def test_module_level_add_event(self):
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key,
                                          {"allowed_operations": ["write"]})
     # client = KeenClient(project_id, write_key=write_key, read_key=read_key)
     keen.add_event("python_test", {"hello": "goodbye"})
Exemplo n.º 12
0
    def test_generate_image_beacon_timestamp(self):
        # make sure using a timestamp works

        keen.project_id = "5004ded1163d66114f000000"
        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        keen.write_key = scoped_keys.encrypt(api_key,
                                             {"allowed_operations": ["write"]})

        event_collection = "python_test"
        event_data = {"a": "b"}
        timestamp = datetime.datetime.utcnow()
        data = self.base64_encode(
            json.dumps({
                "a": "b",
                "keen": {
                    "timestamp": timestamp.isoformat()
                }
            }))

        url = keen.generate_image_beacon(event_collection,
                                         event_data,
                                         timestamp=timestamp)
        expected = "https://api.keen.io/3.0/projects/{0}/events/{1}?api_key={2}&data={3}".format(
            keen.project_id, self.url_escape(event_collection),
            keen.write_key.decode(sys.getdefaultencoding()), data)
        self.assert_equal(expected, url)
Exemplo n.º 13
0
    def test_generate_image_beacon(self):
        keen.project_id = "5004ded1163d66114f000000"
        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        keen.write_key = scoped_keys.encrypt(api_key,
                                             {"allowed_operations": ["write"]})

        event_collection = "python_test hello!?"
        event_data = {"a": "b"}
        data = self.base64_encode(json.dumps(event_data))

        # module level should work
        url = keen.generate_image_beacon(event_collection, event_data)
        expected = "https://api.keen.io/3.0/projects/{0}/events/{1}?api_key={2}&data={3}".format(
            keen.project_id, self.url_escape(event_collection),
            keen.write_key.decode(sys.getdefaultencoding()), data)
        self.assert_equal(expected, url)

        # so should instance level
        client = KeenClient(keen.project_id,
                            write_key=keen.write_key,
                            read_key=None)
        url = client.generate_image_beacon(event_collection, event_data)
        self.assert_equal(expected, url)

        # make sure URL works
        response = requests.get(url)
        self.assert_equal(200, response.status_code)
        self.assert_equal(
            b"GIF89a\x01\x00\x01\x00\x80\x01\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\x01\n\x00\x01\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02L\x01\x00;",
            response.content)
 def test_direct_persistence_strategy(self):
     project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]})
     client = KeenClient(project_id, write_key=write_key, read_key=read_key)
     client.add_event("python_test", {"hello": "goodbye"})
     client.add_event("python_test", {"hello": "goodbye"})
     client.add_events(
         {
             "sign_ups": [{
                 "username": "******",
                 "referred_by": "steve",
                 "son_of": "my_mom"
             }],
             "purchases": [
                 {"price": 5},
                 {"price": 6},
                 {"price": 7}
             ]}
     )
Exemplo n.º 15
0
 def test_direct_persistence_strategy(self):
     project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     write_key = scoped_keys.encrypt(api_key,
                                     {"allowed_operations": ["write"]})
     read_key = scoped_keys.encrypt(api_key,
                                    {"allowed_operations": ["read"]})
     client = KeenClient(project_id, write_key=write_key, read_key=read_key)
     client.add_event("python_test", {"hello": "goodbye"})
     client.add_event("python_test", {"hello": "goodbye"})
     client.add_events({
         "sign_ups": [{
             "username": "******",
             "referred_by": "steve",
             "son_of": "my_mom"
         }],
         "purchases": [{
             "price": 5
         }, {
             "price": 6
         }, {
             "price": 7
         }]
     })
Exemplo n.º 16
0
def generate_write_key():
    """Create scoped key for write access to Keen.io database."""
    if not has_master_key():
        logger.warning("Keen.io Write Key was not created,"
                       " keen.master_key is not defined.")
        return None

    master_key = keen.master_key or os.environ.get("KEEN_MASTER_KEY")

    privileges = {
        "allowed_operations": ["write"]
    }

    logger.info("Keen.io Write Key is created")
    return scoped_keys.encrypt(master_key, privileges)
    def test_generate_image_beacon_timestamp(self):
        # make sure using a timestamp works

        keen.project_id = "5004ded1163d66114f000000"
        api_key = "2e79c6ec1d0145be8891bf668599c79a"
        keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})

        event_collection = "python_test"
        event_data = {"a": "b"}
        timestamp = datetime.datetime.utcnow()
        data = self.base64_encode(json.dumps({"a": "b", "keen": {"timestamp": timestamp.isoformat()}}))

        url = keen.generate_image_beacon(event_collection, event_data, timestamp=timestamp)
        expected = "https://api.keen.io/3.0/projects/{0}/events/{1}?api_key={2}&data={3}".format(
            keen.project_id, self.url_escape(event_collection), keen.write_key.decode(sys.getdefaultencoding()), data
        )
        self.assert_equal(expected, url)
Exemplo n.º 18
0
def generate_read_key(repo):
    """
    Create scoped key for reading only the build-stages related data.

    Param repo : github repository slug (fe. buildtimetrend/python-lib)
    """
    if not has_master_key():
        logger.warning("Keen.io Read Key was not created,"
                       " keen.master_key is not defined.")
        return None

    master_key = keen.master_key or os.environ.get("KEEN_MASTER_KEY")

    privileges = {
        "allowed_operations": ["read"]
    }

    if repo is not None:
        privileges["filters"] = [get_repo_filter(repo)]

    logger.info("Keen.io Read Key is created for %s", repo)
    return scoped_keys.encrypt(master_key, privileges)
Exemplo n.º 19
0
def keen_io_generate_read_key(repo):
    '''
    Create scoped key for reading only the build-stages related data.
    Param repo : github repository slug (fe. ruleant/buildtime-trend)
    '''
    logger = get_logger()

    if "KEEN_MASTER_KEY" in os.environ:
        master_key = os.getenv("KEEN_MASTER_KEY")
        privileges = {
            "filters": [{
                "property_name": "build.repo",
                "operator": "eq",
                "property_value": repo
            }],
            "allowed_operations": ["read"]
        }

        logger.info("Keen.io Read Key is created for %s", repo)
        return scoped_keys.encrypt(master_key, privileges)

    logger.warning("Keen.io Read Key was not created.")
    return None
Exemplo n.º 20
0
from helpers_directive import vary_directives
from helpers_url import get_directives_from_random_matching_block

from keen.client import KeenClient
from keen import scoped_keys

logger = logging.getLogger('crawlbin.pages.views')

keen = KeenClient(
    project_id=settings.KEEN_PROJECT_ID,
    write_key=settings.KEEN_WRITE_KEY,
    read_key=settings.KEEN_READ_KEY,
    master_key=settings.KEEN_MASTER_KEY,
)

scoped_write_key = scoped_keys.encrypt(settings.KEEN_MASTER_KEY, {"allowed_operations": ["write"]})
keeniod_url = "https://api.keen.io/3.0/projects/"+settings.KEEN_PROJECT_ID+\
"/events/distilled_link_clicked?api_key="+scoped_write_key+"&data=e30=&redirect="


def index(request):
    """ Render the crawlbin index page.

    """
    keen.add_event("visit",
        {'page': 'index.html',
        "ip_address": "${keen.ip}",
            "keen": {
            "addons": [{
                "name": "keen:ip_to_geo",
                "input": {"ip": "ip_address"},
Exemplo n.º 21
0
 def test_timeout_count(self):
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]})
     client = KeenClient(keen.project_id, write_key=None, read_key=keen.read_key, get_timeout=0.0001)
     resp = client.count("query test", timeframe="today", filters=self.get_filter())
 def test_module_level_add_events(self):
     keen.project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     # client = KeenClient(project_id, write_key=write_key, read_key=read_key)
     keen.add_events({"python_test": [{"hello": "goodbye"}]})
Exemplo n.º 23
0
 def test_old_scoped_key_encrypts_and_decrypts(self):
     encrypted = scoped_keys.encrypt(self.old_api_key, self.options)
     decrypted = scoped_keys.decrypt(self.old_api_key, encrypted)
     self.assert_equal(decrypted, self.options)
Exemplo n.º 24
0
from helpers_directive import vary_directives
from helpers_url import get_directives_from_random_matching_block

from keen.client import KeenClient
from keen import scoped_keys

logger = logging.getLogger('crawlbin.pages.views')

keen = KeenClient(
    project_id=settings.KEEN_PROJECT_ID,
    write_key=settings.KEEN_WRITE_KEY,
    read_key=settings.KEEN_READ_KEY,
    master_key=settings.KEEN_MASTER_KEY,
)

scoped_write_key = scoped_keys.encrypt(settings.KEEN_MASTER_KEY,
                                       {"allowed_operations": ["write"]})
keeniod_url = "https://api.keen.io/3.0/projects/"+settings.KEEN_PROJECT_ID+\
"/events/distilled_link_clicked?api_key="+scoped_write_key+"&data=e30=&redirect="


def index(request):
    """ Render the crawlbin index page.

    """
    keen.add_event(
        "visit", {
            'page':
            'index.html',
            "ip_address":
            "${keen.ip}",
            "keen": {
Exemplo n.º 25
0
 def test_direct_persistence_strategy(self):
     project_id = "5004ded1163d66114f000000"
     api_key = "2e79c6ec1d0145be8891bf668599c79a"
     write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]})
     client = KeenClient(project_id, write_key=write_key)
     client.add_event("python_test", {"hello": "goodbye"})
Exemplo n.º 26
0
from keen.client import KeenClient
from keen import scoped_keys
    
api_key='7EC010A4C033AF0EAC60B13AEA0EF1D7'
    
write_key = scoped_keys.encrypt(api_key, {"filters": [{
                                                      "property_name": "unit_domain",
                                                      "operator": "eq",
                                                      "property_value": "fem-inc.com"
                                                      }],"allowed_operations": ["write"]})
read_key = scoped_keys.encrypt(api_key, {"filters": [{
                                                     "property_name": "unit_domain",
                                                     "operator": "eq",
                                                     "property_value": "fem-inc.com"
                                                     }],"allowed_operations": ["read"]})


print(write_key)
print("=========")
print(read_key)