MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / addACLLogEntry

Function addACLLogEntry

src/acl.cpp:1812–1881  ·  view source on GitHub ↗

Adds a new entry in the ACL log, making sure to delete the old entry * if we reach the maximum length allowed for the log. This function attempts * to find similar entries in the current log in order to bump the counter of * the log entry instead of creating many entries for very similar ACL * rules issues. * * The argpos argument is used when the reason is ACL_DENIED_KEY or * ACL_DENIED_C

Source from the content-addressed store, hash-verified

1810 * reason. Otherwise it will just be NULL.
1811 */
1812void addACLLogEntry(client *c, int reason, int argpos, sds username) {
1813 /* Create a new entry. */
1814 struct ACLLogEntry *le = (ACLLogEntry*)zmalloc(sizeof(*le));
1815 le->count = 1;
1816 le->reason = reason;
1817 le->username = sdsdup(reason == ACL_DENIED_AUTH ? username : c->user->name);
1818 le->ctime = mstime();
1819
1820 switch(reason) {
1821 case ACL_DENIED_CMD: le->object = sdsnew(c->cmd->name); break;
1822 case ACL_DENIED_KEY: le->object = sdsdup(szFromObj(c->argv[argpos])); break;
1823 case ACL_DENIED_CHANNEL: le->object = sdsdup(szFromObj(c->argv[argpos])); break;
1824 case ACL_DENIED_AUTH: le->object = sdsdup(szFromObj(c->argv[0])); break;
1825 default: le->object = sdsempty();
1826 }
1827
1828 client *realclient = c;
1829 if (realclient->flags & CLIENT_LUA) realclient = g_pserver->lua_caller;
1830
1831 le->cinfo = catClientInfoString(sdsempty(),realclient);
1832 if (c->flags & CLIENT_MULTI) {
1833 le->context = ACL_LOG_CTX_MULTI;
1834 } else if (c->flags & CLIENT_LUA) {
1835 le->context = ACL_LOG_CTX_LUA;
1836 } else {
1837 le->context = ACL_LOG_CTX_TOPLEVEL;
1838 }
1839
1840 /* Try to match this entry with past ones, to see if we can just
1841 * update an existing entry instead of creating a new one. */
1842 long toscan = 10; /* Do a limited work trying to find duplicated. */
1843 listIter li;
1844 listNode *ln;
1845 listRewind(ACLLog,&li);
1846 ACLLogEntry *match = NULL;
1847 while (toscan-- && (ln = listNext(&li)) != NULL) {
1848 ACLLogEntry *current = (ACLLogEntry*)listNodeValue(ln);
1849 if (ACLLogMatchEntry(current,le)) {
1850 match = current;
1851 listDelNode(ACLLog,ln);
1852 listAddNodeHead(ACLLog,current);
1853 break;
1854 }
1855 }
1856
1857 /* If there is a match update the entry, otherwise add it as a
1858 * new one. */
1859 if (match) {
1860 /* We update a few fields of the existing entry and bump the
1861 * counter of events for this entry. */
1862 sdsfree(match->cinfo);
1863 match->cinfo = le->cinfo;
1864 match->ctime = le->ctime;
1865 match->count++;
1866
1867 /* Release the old entry. */
1868 le->cinfo = NULL;
1869 ACLFreeLogEntry(le);

Callers 4

execCommandFunction · 0.85
processCommandFunction · 0.85
ACLAuthenticateUserFunction · 0.85
luaRedisGenericCommandFunction · 0.85

Calls 14

zmallocFunction · 0.85
sdsdupFunction · 0.85
sdsnewFunction · 0.85
szFromObjFunction · 0.85
sdsemptyFunction · 0.85
catClientInfoStringFunction · 0.85
listRewindFunction · 0.85
listNextFunction · 0.85
ACLLogMatchEntryFunction · 0.85
listDelNodeFunction · 0.85
listAddNodeHeadFunction · 0.85
sdsfreeFunction · 0.85

Tested by

no test coverage detected