You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spot.apache.org by ev...@apache.org on 2017/03/29 16:51:33 UTC

[09/50] [abbrv] incubator-spot git commit: Proxy migrated to GraphQL

Proxy migrated to GraphQL


Project: http://git-wip-us.apache.org/repos/asf/incubator-spot/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-spot/commit/f37bb403
Tree: http://git-wip-us.apache.org/repos/asf/incubator-spot/tree/f37bb403
Diff: http://git-wip-us.apache.org/repos/asf/incubator-spot/diff/f37bb403

Branch: refs/heads/SPOT-35_graphql_api
Commit: f37bb403e5e3a55f88a3c291f4bc2c652d6bf505
Parents: 52f34f4
Author: Diego Ortiz <di...@intel.com>
Authored: Mon Mar 6 10:53:13 2017 -0600
Committer: Diego Ortiz Huerta <di...@intel.com>
Committed: Wed Mar 15 11:49:47 2017 -0700

----------------------------------------------------------------------
 spot-oa/api/graphql/common.py                 |   4 +-
 spot-oa/api/graphql/proxy/mutation.py         | 113 ++++++++++++++++-----
 spot-oa/api/graphql/proxy/query.py            |  38 ++++---
 spot-oa/api/graphql/schema.py                 |   4 +
 spot-oa/ui/proxy/js/stores/SuspiciousStore.js |  10 +-
 spot-oa/ui/proxy/js/stores/TimelineStore.js   |   2 +-
 6 files changed, 122 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/api/graphql/common.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/graphql/common.py b/spot-oa/api/graphql/common.py
index 02568c2..2c2e9bd 100644
--- a/spot-oa/api/graphql/common.py
+++ b/spot-oa/api/graphql/common.py
@@ -23,7 +23,7 @@ def coerce_date(value):
         return datetime.strptime(str(value), '%Y-%m-%d').date()
 
 def serialize_date(value):
-    return date.strptime(value, '%Y-%m-%d').strftime('%Y-%m-%d')
+    return datetime.strptime(value, '%Y-%m-%d').strftime('%Y-%m-%d')
 
 def parse_date_literal(ast):
     return datetime.strptime(ast.value, '%Y-%m-%d')
@@ -115,7 +115,7 @@ IngestSummaryType = GraphQLObjectType(
     fields={
         'datetime': GraphQLField(
             type=SpotDatetimeType,
-            resolver=lambda root, *_: root.get('tdate')
+            resolver=lambda root, *_: '{}:00'.format(root.get('tdate'))
         ),
         'total': GraphQLField(
             type=GraphQLInt,

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/api/graphql/proxy/mutation.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/graphql/proxy/mutation.py b/spot-oa/api/graphql/proxy/mutation.py
index ffd8ea1..09e9fe4 100644
--- a/spot-oa/api/graphql/proxy/mutation.py
+++ b/spot-oa/api/graphql/proxy/mutation.py
@@ -3,6 +3,7 @@ from graphql import (
     GraphQLObjectType,
     GraphQLField,
     GraphQLArgument,
+    GraphQLList,
     GraphQLString,
     GraphQLInt,
     GraphQLNonNull,
@@ -10,7 +11,7 @@ from graphql import (
     GraphQLInputObjectField
 )
 
-from api.graphql.common import SpotDateType, SpotIpType, SpotOperationOutputType
+from api.graphql.common import SpotDateType, SpotDatetimeType, SpotIpType, SpotOperationOutputType
 import api.resources.proxy as Proxy
 
 ScoreInputType = GraphQLInputObjectType(
@@ -25,18 +26,67 @@ ScoreInputType = GraphQLInputObjectType(
             description='A score value, 1->High, 2->Medium, 3->Low'
         ),
         'uri': GraphQLInputObjectField(
-            type=GraphQLString,
-            description='Requested URI'
+            type=GraphQLNonNull(GraphQLString),
+            description='Full URI'
+        )
+    }
+)
+
+ThreatDetailsInputType = GraphQLInputObjectType(
+    name='ProxyThreatDetailsInputType',
+    fields={
+        'datetime': GraphQLInputObjectField(
+            type=SpotDatetimeType
         ),
         'clientIp': GraphQLInputObjectField(
-            type=SpotIpType,
-            description='Client\'s ip'
+            type=SpotIpType
+        ),
+        'username': GraphQLInputObjectField(
+            type=GraphQLString
+        ),
+        'duration': GraphQLInputObjectField(
+            type=GraphQLInt
+        ),
+        'uri': GraphQLInputObjectField(
+            type=GraphQLString
+        ),
+        'webCategory': GraphQLInputObjectField(
+            type=GraphQLString
+        ),
+        'responseCode': GraphQLInputObjectField(
+            type=GraphQLInt
+        ),
+        'requestMethod': GraphQLInputObjectField(
+            type=GraphQLString,
+            description='Http Method'
+        ),
+        'userAgent': GraphQLInputObjectField(
+            type=GraphQLString,
+            description='Client\'s user agent'
+        ),
+        'responseContentType': GraphQLInputObjectField(
+            type=GraphQLString
+        ),
+        'referer': GraphQLInputObjectField(
+            type=GraphQLString
+        ),
+        'uriPort': GraphQLInputObjectField(
+            type=GraphQLInt
+        ),
+        'serverIp': GraphQLInputObjectField(
+            type=SpotIpType
+        ),
+        'serverToClientBytes': GraphQLInputObjectField(
+            type=GraphQLInt
+        ),
+        'clientToServerBytes': GraphQLInputObjectField(
+            type=GraphQLInt
         )
     }
 )
 
-AddCommentInputType = GraphQLInputObjectType(
-    name='ProxyAddCommentInputType',
+CreateStoryboardInputType = GraphQLInputObjectType(
+    name='ProxyCreateStoryboardInputType',
     fields={
         'date': GraphQLInputObjectField(
             type=SpotDateType,
@@ -53,53 +103,66 @@ AddCommentInputType = GraphQLInputObjectType(
         'text': GraphQLInputObjectField(
             type=GraphQLNonNull(GraphQLString),
             description='A description text for the comment'
+        ),
+        'threatDetails': GraphQLInputObjectField(
+            type=GraphQLNonNull(GraphQLList(GraphQLNonNull(ThreatDetailsInputType))),
+        ),
+        'first': GraphQLInputObjectField(
+            type=GraphQLInt
         )
     }
 )
 
-def _score_connection(args):
+def _score_connections(args):
+    results = []
+
     _input = args.get('input')
-    _date = _input.get('date', date.today())
-    score = _input.get('score')
-    uri = _input.get('uri')
-    clientIp = _input.get('clientIp')
+    for cmd in _input:
+        _date = cmd.get('date', date.today())
+        score = cmd.get('score')
+        uri = cmd.get('uri')
 
-    return {'success': Proxy.score_request(date=_date, score=score, uri=uri, cllientip=clientIp)}
+        result = Proxy.score_request(date=_date, score=score, uri=uri)
 
-def _add_comment(args):
+        results.append({'success': result})
+
+    return results
+
+def _create_storyboard(args):
     _input = args.get('input')
     _date = _input.get('date', date.today())
     uri = _input.get('uri')
     title = _input.get('title')
     text = _input.get('text')
+    threat_details = _input.get('threatDetails')
+    first = _input.get('first')
 
-    if Proxy.save_comment(date=_date, uri=uri, title=title, text=text) is None:
-        return {'success':True}
-    else:
-        return {'success':False}
+    result = Proxy.create_storyboard(date=_date, uri=uri, title=title, text=text, expanded_search=threat_details, top_results=first)
+
+    return {'success': result}
 
 MutationType = GraphQLObjectType(
     name='ProxyMutationType',
     fields={
         'score': GraphQLField(
-            type=SpotOperationOutputType,
+            type=GraphQLList(SpotOperationOutputType),
             args={
                 'input': GraphQLArgument(
-                    type=GraphQLNonNull(ScoreInputType),
+                    type=GraphQLNonNull(GraphQLList(GraphQLNonNull(ScoreInputType))),
                     description='Score criteria'
                 )
             },
-            resolver=lambda root, args, *_: _score_connection(args)
+            resolver=lambda root, args, *_: _score_connections(args)
         ),
-        'addComment': GraphQLField(
+        'createStoryboard': GraphQLField(
             type=SpotOperationOutputType,
             args={
                 'input': GraphQLArgument(
-                    type=GraphQLNonNull(AddCommentInputType),
-                    description='Comment info'
+                    type=GraphQLNonNull(CreateStoryboardInputType),
+                    description='Generates every data needed to move a threat to the storyboard'
                 )
             },
-            resolver=lambda root, args, *_: _add_comment(args)
+            resolver=lambda root, args, *_: _create_storyboard(args)
         )
     }
 )

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/api/graphql/proxy/query.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/graphql/proxy/query.py b/spot-oa/api/graphql/proxy/query.py
index aa16fc8..d75a1df 100644
--- a/spot-oa/api/graphql/proxy/query.py
+++ b/spot-oa/api/graphql/proxy/query.py
@@ -18,7 +18,7 @@ SuspiciousType = GraphQLObjectType(
         'datetime': GraphQLField(
             type=SpotDatetimeType,
             description='Start time of the request',
-            resolver=lambda root, *_: '{} {}'.format(root.get('p_date', ''), root.get('p_time', ''))
+            resolver=lambda root, *_: '{} {}'.format(root.get('tdate') or '1970-01-01', root.get('time') or '00:00:00')
         ),
         'clientIp': GraphQLField(
             type=SpotIpType,
@@ -107,7 +107,8 @@ SuspiciousType = GraphQLObjectType(
         ),
         'score': GraphQLField(
             type=GraphQLInt,
-            resolver=lambda root, *_: root.get('score')
+            description='Score value assigned by machine learning algorithm',
+            resolver=lambda root, *_: root.get('ml_score') or 0
         ),
         'uriRep': GraphQLField(
             type=GraphQLString,
@@ -132,7 +133,7 @@ EdgeDetailsType = GraphQLObjectType(
         'datetime': GraphQLField(
             type=GraphQLString,
             description='Start time of the request',
-            resolver=lambda root, *_: '{} {}'.format(root.get('p_date'), root.get('p_time'))
+            resolver=lambda root, *_: '{} {}'.format(root.get('tdate') or '1970-01-01', root.get('time') or '00:00:00')
         ),
         'clientIp': GraphQLField(
             type=SpotIpType,
@@ -210,15 +211,20 @@ EdgeDetailsType = GraphQLObjectType(
 ScoredRequestType = GraphQLObjectType(
     name='ProxyScoredRequestType',
     fields={
+        'datetime': GraphQLField(
+            type=SpotDateType,
+            description='Date and time of user score',
+            resolver=lambda root, *_: root.get('tdate') or '1970-01-01'
+        ),
         'uri': GraphQLField(
             type=SpotIpType,
             description='Requested URI',
-            resolver=lambda root, *_: root.get('uri')
+            resolver=lambda root, *_: root.get('fulluri')
         ),
         'score': GraphQLField(
             type=GraphQLInt,
             description='Score value. 1->High, 2->Medium, 3->Low',
-            resolver=lambda root, *_: root.get('score') or 0
+            resolver=lambda root, *_: root.get('uri_sev') or 0
         )
     }
 )
@@ -228,7 +234,7 @@ CommentType = GraphQLObjectType(
     fields={
         'uri': GraphQLField(
             type=GraphQLString,
-            resolver=lambda root, *_: root.get('uri_threat')
+            resolver=lambda root, *_: root.get('p_threat')
         ),
         'title': GraphQLField(
             type=GraphQLString,
@@ -253,7 +259,7 @@ ThreatsInformationType = GraphQLObjectType(
                     description='A date to use as reference to retrieve the list of scored requests. Defaults to today'
                 )
             },
-            resolver=lambda root, args, *_: Proxy.get_scored_request(date=args.get('date', date.today()))
+            resolver=lambda root, args, *_: Proxy.get_scored_requests(date=args.get('date', date.today()))
         ),
         'comments': GraphQLField(
             type=GraphQLList(CommentType),
@@ -269,12 +275,12 @@ ThreatsInformationType = GraphQLObjectType(
     }
 )
 
-ExpandedSearchType = GraphQLObjectType(
-    name='DnsExpandedSearchType',
+ThreatDetailsType = GraphQLObjectType(
+    name='ProxyThreatDetailsType',
     fields={
         'datetime': GraphQLField(
             type=SpotDatetimeType,
-            resolver=lambda root, *_: root.get('p_time')
+            resolver=lambda root, *_: '{} {}'.format(root.get('p_date') or '1970-01-01', root.get('p_time') or '00:00:00')
         ),
         'clientIp': GraphQLField(
             type=SpotIpType,
@@ -389,11 +395,11 @@ TimelineType = GraphQLObjectType(
     fields={
         'startDatetime': GraphQLField(
             type=SpotDatetimeType,
-            resolver=lambda root, *_: root.get('tstart')
+            resolver=lambda root, *_: root.get('tstart') or '1970-01-01 00:00:00'
         ),
         'endDatetime': GraphQLField(
             type=SpotDatetimeType,
-            resolver=lambda root, *_: root.get('tend')
+            resolver=lambda root, *_: root.get('tend') or '1970-01-01 00:00:00'
         ),
         'duration': GraphQLField(
             type=GraphQLInt,
@@ -406,6 +412,10 @@ TimelineType = GraphQLObjectType(
         'responseCode': GraphQLField(
             type=GraphQLInt,
             resolver=lambda root, *_: root.get('respcode')
+        ),
+        'responseCodeLabel': GraphQLField(
+            type=GraphQLString,
+            resolver=lambda root, *_: root.get('respcode_name')
         )
     }
 )
@@ -414,7 +424,7 @@ ThreatInformationType = GraphQLObjectType(
     name='ProxyThreatInformation',
     fields={
         'details': GraphQLField(
-            type=GraphQLList(ExpandedSearchType),
+            type=GraphQLList(ThreatDetailsType),
             description='Detailed information about a high risk threat',
             args={
                 'date': GraphQLArgument(
@@ -500,7 +510,7 @@ QueryType = GraphQLObjectType(
                     description='Client\'s ip'
                 )
             },
-            resolver=lambda root, args, *_: Proxy.details(date=args.get('date', date.today()), uri=args.get('uri'), clientip=args.get('clientIp'))
+            resolver=lambda root, args, *_: Proxy.details(date=args.get('date', date.today()), uri=args.get('uri'), ip=args.get('clientIp'))
         ),
         'threats': GraphQLField(
             type=ThreatsInformationType,

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/api/graphql/schema.py
----------------------------------------------------------------------
diff --git a/spot-oa/api/graphql/schema.py b/spot-oa/api/graphql/schema.py
index 06a238b..3975b6c 100644
--- a/spot-oa/api/graphql/schema.py
+++ b/spot-oa/api/graphql/schema.py
@@ -39,6 +39,10 @@ SpotSchema = GraphQLSchema(
         'dns': GraphQLField(
             type=DnsMutationType,
             resolver=lambda *_: {}
+        ),
+        'proxy': GraphQLField(
+            type=ProxyMutationType,
+            resolver=lambda *_: {}
         )
     }
   ),

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/ui/proxy/js/stores/SuspiciousStore.js
----------------------------------------------------------------------
diff --git a/spot-oa/ui/proxy/js/stores/SuspiciousStore.js b/spot-oa/ui/proxy/js/stores/SuspiciousStore.js
index 1b3141e..422eb1a 100755
--- a/spot-oa/ui/proxy/js/stores/SuspiciousStore.js
+++ b/spot-oa/ui/proxy/js/stores/SuspiciousStore.js
@@ -15,10 +15,6 @@ const HIGHLIGHT_THREAT_EVENT = 'hightlight_thread';
 const UNHIGHLIGHT_THREAT_EVENT = 'unhightlight_thread';
 const SELECT_THREAT_EVENT = 'select_treath';
 
-var filterName = '';
-var highlightedThread = null;
-var selectedThread = null;
-
 class SuspiciousStore extends ObservableWithHeadersGraphQLStore {
     constructor() {
         super();
@@ -87,8 +83,8 @@ class SuspiciousStore extends ObservableWithHeadersGraphQLStore {
             this.unsetVariable(CLIENT_IP_VAR);
         }
         else if (SpotUtils.IP_V4_REGEX.test(filter)) {
-            this.setVariable(URI_VAR, filter);
-            this.unsetVariable(CLIENT_IP_VAR, filter);
+            this.unsetVariable(URI_VAR, filter);
+            this.setVariable(CLIENT_IP_VAR, filter);
         }
         else {
             this.unsetVariable(CLIENT_IP_VAR);
@@ -99,7 +95,7 @@ class SuspiciousStore extends ObservableWithHeadersGraphQLStore {
     }
 
     getFilter() {
-        return this.getVariable(CLIENT_IP_VAR) || this.getVariable(URI_VAR);
+        return this.getVariable(CLIENT_IP_VAR) || this.getVariable(URI_VAR) || '';
     }
 
     addChangeFilterListener(callback) {

http://git-wip-us.apache.org/repos/asf/incubator-spot/blob/f37bb403/spot-oa/ui/proxy/js/stores/TimelineStore.js
----------------------------------------------------------------------
diff --git a/spot-oa/ui/proxy/js/stores/TimelineStore.js b/spot-oa/ui/proxy/js/stores/TimelineStore.js
index 952de48..54d7b48 100755
--- a/spot-oa/ui/proxy/js/stores/TimelineStore.js
+++ b/spot-oa/ui/proxy/js/stores/TimelineStore.js
@@ -18,7 +18,7 @@ class TimelineStore extends ObservableGraphQLStore {
                             duration
                             clientip: clientIp
                             tend: endDatetime
-                            respcode: responseCode
+                            respcode: responseCodeLabel
                             tstart: startDatetime
                         }
                     }